加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c++ 数据类型

发布时间:2020-12-15 04:53:31 所属栏目:百科 来源:网络整理
导读:1. 基本数据类型 2. 抽象数据类型标准库:string,vector,bitset string和vecotr分别定义大小可变的字符串和集合。string和vector往往将迭代器用作配套类型,用于访问string中的字符或vector中的元素。 string类类型和许多其他库类型都定义了一些配套类型(

1. 基本数据类型

2. 抽象数据类型标准库:string,vector,bitset

string和vecotr分别定义大小可变的字符串和集合。string和vector往往将迭代器用作配套类型,用于访问string中的字符或vector中的元素。

string类类型和许多其他库类型都定义了一些配套类型(companion type),通过这些配套类型,酷类型的使用就能与机器无光。size_type就是配套类型中的一种,它定义为与unsigned型(unsigned int或unsigned long)具有相同的含义,而且可以保证足够大能够存储任意string对象的长度。

为了使用由string类型定义的size_type类型,必须加上作用域操作符来说明所使用的size_type类型是由string类定义的。

遇到很神奇的代码bug,leetcode上第二题,VS中可以运行通过,而leetcode上不行,不知道为什么?先放在这里,后面知道了问题所在再解决。

/**

* Definition for singly-linked list.

*/

#include

#include

using namespace std;

struct ListNode {

int val;

ListNode *next;

ListNode(int x) : val(x),next(NULL) {}

};

ListNode* addTwoNumbers(ListNode* l1,ListNode* l2) {

bool flag = false;

static vector mem;

ListNode* result = NULL;

while (l1 != NULL || l2 != NULL)

{

int sum = int(flag);

if (l1 != NULL)

{

sum += l1->val;

l1 = l1->next;

}

if (l2 != NULL)

{

sum += l2->val;

l2 = l2->next;

}

if (sum / 10)

{

flag = true;

sum %= 10;

}

else

{

flag = false;

}

ListNode a(sum);

mem.push_back(a);

}

if (flag)

{

ListNode a(1);

mem.push_back(a);

}

for (vector::size_type ix = 1; ix != mem.size(); ix++)

{

mem[ix - 1].next = &mem[ix];

}

result = &(mem[0]);

return result;

}

int main()

{

ListNode* l1 = new ListNode[3]{ 2,4,3 };

for (int i = 0; i < 2; i++)

{

l1[i].next = &l1[i + 1];

printf("%d ",l1[i].val);

}

ListNode* l2 = new ListNode[3]{ 5,6,4 };

for (int i = 0; i < 2; i++)

{

l2[i].next = &l2[i + 1];

printf("%d ",l2[i].val);

}

ListNode* result = addTwoNumbers(l1,l2);

while (result)

{

printf("%dt",result->val);

result = result->next;

}

return 0;

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读