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- ed list.
*/ 
#include <iostream>
#include <vector>
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<ListNode> 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<ListNode>::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(\"%d\\t\", result->val);
			result = result->next;
		}
		return 0;
	}

 

收藏 打印