第九章 顺序容器

9.1

(a):list,因为要频繁地进行任意位置的插入操作

(b):deque,因为只要在头尾位置进行插入/删除操作(相比于list,deque还支持快速随机访问)

(c):vector,没有要求进行插入/删除操作

9.2

	list<deque<int>> l;		//元素类型为另一种容器类型deque

 

9.3

两个迭代器begin和end构成一个迭代器范围。

两条限制:

  1. 它们指向同一个容器中的元素,或者是容器最后一个元素之后的位置;
  2. end可以与begin指向相同的位置,但不能指向begin之前的位置。

9.4

 1 #include <iostream>
 2 #include <vector> 
 3 #include <string>
 4 #include <deque>
 5 #include <list> 
 6  
 7 using namespace std;
 8 
 9 bool is_find(vector<int>::iterator beg, vector<int>::iterator end, int x)
10 {
11     while (beg != end) {
12         if (*beg == x)
13             return true;
14         ++beg; 
15     }
16     return false;
17 }
18 
19 int main()
20 { 
21     vector<int> vec{1, 2, 3, 4, 5};
22     auto p1 = vec.begin(), p2 = vec.end();
23     bool flag = is_find(p1, p2, 5);
24     if (flag)
25         cout << "Found!
";
26     else
27         cout << "Not Found!
";
28     return 0;
29 }
View Code

9.5

 1 #include <iostream>
 2 #include <vector> 
 3 #include <string>
 4 #include <deque>
 5 #include <list> 
 6  
 7 using namespace std;
 8 
 9 vector<int>::iterator find_num(vector<int>::iterator beg, vector<int>::iterator end, int x)
10 {
11     while (beg != end) {
12         if (*beg == x)
13             return beg;
14         ++beg; 
15     }
16     return end;
17 }
18 
19 int main()
20 { 
21     vector<int> vec{1, 2, 3, 4, 5};
22     auto p1 = vec.begin(), p2 = vec.end();
23     auto it = find_num(p1, p2, 5);
24     if (it != p2)
25         cout << "Found!
";
26     else
27         cout << "Not Found!
";
28     return 0;
29 }
View Code

9.6

list的迭代器不支持<运算,只支持递增递减、判等、解引用运算符,故可修改如下:

	while (list1 != list2) /* ... */

 

9.7

	vector<int>::iterator it;

 

9.8

	//读取容器中的元素 
	list<string>::value_type
	//写入容器中的元素 
	list<string>::reference

 

9.9

当auto与begin结合使用时,获得的迭代器类型依赖于容器类型;

当auto与cbegin结合使用时,获得的一定是const_iterator(无论容器类型是什么)。

当不需要写访问容器时,应使用cbegin。

9.10

v1是int的vector类型,可以修改v1的内容,包括添加、删除元素以及修改元素等操作。

v2是int的常量vector类型,其内容不能修改,添加、删除元素以及修改元素值等均不允许。

it1是普通迭代器,可对容器元素进行读访问;it2是const迭代器,不能对容器元素进行写访问。

it3和it4都是const迭代器。

9.11

	//way1:创建一个空vector对象vec1 
	vector<int> vec1;
	//way2:创建一个vector对象vec2,并以vec1的拷贝来初始化vec2 
	vector<int> vec2(vec1);
	vector<int> vec3 = vec1;
	//way3:创建一个vector对象vec3,并初始化为迭代器beg和end组成的迭代器范围中的元素的拷贝 
	auto beg = vec1.begin(), end = vec1.end();
	vector<int> vec4(beg, end);
	//way4:列表初始化,vec5含有3个int型元素 
	vector<int> vec5{0, 1, 2};
	vector<int> vec6 = {0, 1, 2};
	//way5:vec7含有3个int型元素,但它们都是值初始化 
	vector<int> vec7(3);
	//way6:vec8含有3个int型元素,且值都为0 
	vector<int> vec8(3, 0);

 

9.12

接收一个容器创建其拷贝的构造函数:两个容器的类型及其元素类型必须匹配;

接收两个迭代器创建拷贝的构造函数:迭代器所属容器可与创建容器的类型不同,并且两个容器中的元素类型也可以不同,只要能将要拷贝的元素转换为创建容器的元素类型即可。

9.13

	list<int> l = {0, 1, 2};
	auto beg = l.begin(), end = l.end();
	vector<double> vec1(beg, end); 
	vector<int> vec = {0, 1, 2};
	auto beg = vec.begin(), end = vec.end();
	vector<double> vec1(beg, end); 

 

9.14

	list<const char*> l = {"hello", "world"};
	vector<string> vec;
	vec = l;	//错误,两者容器类型不同
	vec.assign(l.begin(), l.end()); 	//正确

 

9.15

 1 #include <iostream>
 2 #include <vector> 
 3 #include <string>
 4 #include <deque>
 5 #include <list> 
 6  
 7 using namespace std;
 8 
 9 int main()
10 { 
11     vector<int> vec1;
12     vector<int> vec2;
13     if (vec1 == vec2)
14         cout << "Equal!
";
15     else
16         cout << "Not Equal!
";
17     return 0;
18 }
View Code

9.16

 1 #include <iostream>
 2 #include <vector> 
 3 #include <string>
 4 #include <deque>
 5 #include <list> 
 6  
 7 using namespace std;
 8 
 9 void compare(vector<int> v, list<int> l)
10 {
11     if (v.size() != l.size()) {
12         cout << "Not Equal!
";
13         return;
14     }
15     else {
16         auto iter = l.begin();
17         for (auto it : v) {
18             if (it != *iter) {
19                 cout << "Not equal
";
20                 return;
21             }
22             ++iter;
23         }
24         cout << "Equal!
";
25     }
26 }
27 
28 int main()
29 { 
30     vector<int> vec(3, 0);
31     list<int> l(3, 0);
32     compare(vec, l);
33     return 0;
34 }
View Code

9.17

限制:

两个容器不能是无序关联容器;

两个容器的类型必须一致,并且容器内的元素类型也要一致;

两个容器的元素类型也要支持<运算符。

9.18

 1 #include <iostream>
 2 #include <vector> 
 3 #include <string>
 4 #include <deque>
 5 #include <list> 
 6  
 7 using namespace std;
 8 
 9 int main()
10 { 
11     deque<string> deq;
12     string ss;
13     while (cin >> ss)
14         deq.push_back(ss);
15     for (deque<string>::iterator it = deq.begin(); it != deq.end(); ++it) {
16         cout << *it << endl;
17     }
18     return 0;
19 }
View Code

9.19

 1 #include <iostream>
 2 #include <vector> 
 3 #include <string>
 4 #include <deque>
 5 #include <list> 
 6  
 7 using namespace std;
 8 
 9 int main()
10 { 
11     list<string> deq;        //一处 
12     string ss;
13     while (cin >> ss)
14         deq.push_back(ss);
15     for (list<string>::iterator it = deq.begin(); it != deq.end(); ++it) {    //两处 
16         cout << *it << endl;
17     }
18     return 0;
19 }
View Code

9.20

 1 #include <iostream>
 2 #include <vector> 
 3 #include <string>
 4 #include <deque>
 5 #include <list> 
 6  
 7 using namespace std;
 8 
 9 int main()
10 { 
11     list<int> lis;
12     deque<int> deq1, deq2;
13     for (auto it = lis.begin(); it != lis.end(); ++it) {
14         if (*it % 2)    //解引用运算符的优先级高于算术运算符 
15             deq1.push_back(*it);
16         else
17             deq2.push_back(*it);
18     }
19     return 0;
20 }
View Code

9.21

大致意思如此:

	string word;
	vector<string> vec;
	auto iter = vec.begin();
	while (cin >> word) 
		iter = vec.insert(iter, word);

在循环之前:程序会创建一个空的vector对象vec,然后定义一个vector<int>类型的迭代器iter,并让它指向vec的首位置。

循环过程:每次在vec的首位置之前插入一个string对象(这个对象成为首位置),然后迭代器iter失效,但函数insert会返回一个指向首位置的迭代器,而循环中正是将这个迭代器返回给iter。

9.22

错误:iter的值并未改变,所以会陷入无意义的循环中。

知识点:向一个vector、string或deque插入元素会使所有指向容器的迭代器、引用和指针失效。

错误:向vector中插入元素使得迭代器iter、mid失效,所以循环也就无意义。

修改:

	int some_val = 1;
	vector<int> iv{0, 1, 2, 3, 4};
	auto iter = iv.begin();
	int i = 0, mid = iv.size() / 2;
	while (i != mid) {
		if (*iter == some_val) {
			iter = iv.insert(iter, 2 * some_val);
			++iter;		//将iter指向原来指向的元素的后一个元素 
			++iter;
		}
		++i; 
	}

 

9.23

val = val2 = val3 = val4 = c中唯一的元素的值

9.24

 1 #include <iostream>
 2 #include <vector> 
 3 #include <string>
 4 #include <deque>
 5 #include <list> 
 6  
 7 using namespace std;
 8 
 9 int main()
10 { 
11     vector<int> vec;
12     int a1 = vec.at(0);
13     int a2 = vec[0];
14     int a3 = vec.front();
15     int a4 = *vec.begin();
16     return 0;
17 }
View Code

9.25

elem1==elem2:不删除任何元素,且返回一个与elem1指向相同元素的迭代器

elem1==elem2==ilist.end():不删除任何元素,且返回一个指向尾后元素的迭代器

9.26

 1 #include <iostream>
 2 #include <iterator>
 3 #include <vector> 
 4 #include <string>
 5 #include <deque>
 6 #include <list> 
 7  
 8 using namespace std;
 9 
10 int main()
11 { 
12     int ia[] = {0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89};
13     /*
14     vector<int> vec;
15     list<int> lis;
16     for (auto &i : ia) {
17         vec.push_back(i);
18         lis.push_back(i);
19     }
20     */
21     vector<int> vec(begin(ia), end(ia));
22     list<int> lis(begin(ia), end(ia));
23     for (auto it = vec.begin(); it != vec.end();) {
24         if (*it % 2 == 0)
25             it = vec.erase(it);
26         else
27             ++it;
28     }
29     for (auto it = lis.begin(); it != lis.end();) {
30         if (*it % 2)
31             it = lis.erase(it);
32         else
33             ++it;
34     }
35     return 0;
36 }
View Code

9.27

 1 #include <iostream>
 2 #include <iterator>
 3 #include <vector> 
 4 #include <string>
 5 #include <deque>
 6 #include <list> 
 7 #include <forward_list>
 8  
 9 using namespace std;
10 
11 int main()
12 { 
13     forward_list<int> flst = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
14     auto it = flst.begin(), itPrev = flst.before_begin();
15     while (it != flst.end()) {
16         if (*it % 2) {
17             cout << *it << endl;
18             it = flst.erase_after(itPrev);
19         }
20         else {
21             ++it;
22             ++itPrev;
23         }
24     }
25     return 0;
26 }
View Code

9.28

 1 #include <iostream>
 2 #include <iterator>
 3 #include <vector> 
 4 #include <string>
 5 #include <deque>
 6 #include <list> 
 7 #include <forward_list>
 8  
 9 using namespace std;
10 
11 void func(forward_list<string> &flst, string &s1, string &s2) {
12     auto cur = flst.begin();
13     auto pre = flst.before_begin();
14     bool flag = false;
15     while (cur != flst.end()) {
16         if (*cur == s1) {
17             flag = true;
18             cur = flst.insert_after(cur, s2);
19         }
20         pre = cur;
21         ++cur;
22     }
23     if (!flag)
24         flst.insert_after(pre, s2);        //此时pre指向尾元素 
25 }
26 
27 int main()
28 { 
29     forward_list<string> flst = {"Hello", "love", "my", "goddess", "kzw", "haha"};
30     string s1("love"), s2("kzw");
31     func(flst, s1, s2);
32     for(auto i: flst)  
33         cout<<i<<" ";  
34     cout<<endl;
35     return 0;
36 }
View Code

9.29

vec.resize(100):会向vec末尾添加75个元素,这些元素将进行值初始化

vec.resize(10):会删除vec末尾的90个元素

9.30

前提:使用relize向容器中添加新元素

(若元素类型不是类类型,则新元素进行值初始化)如果元素类型是类类型,那该类型必须要有默认构造函数(否则不能只接受单个参数)。

9.31

list类型和forward_list类型的迭代器不支持复合赋值运算,可改为两次自加:

 1     list<int> lis = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
 2     auto iter = lis.begin();
 3     while (iter != lis.end()) {
 4         if (*iter % 2) {
 5             iter = lis.insert(iter, *iter);
 6             ++iter;
 7             ++iter;
 8         }
 9         else
10             iter = lis.erase(iter);
11     } 
View Code

9.32

不合法,因为编译器会先计算“*iter++”的值,*iter++这个表达式的值为*iter的值,然而此后iter却指向了下一个元素,故vi.insert(iter, *iter++)将在下一个元素的前面插入*iter,与我们题意相悖(若容器中最后一个数是奇数,更是会使程序异常结束)。

9.33

向vector容器插入元素后,指向插入位置之后元素的迭代器将失效。故迭代器begin和end返回的迭代器将失效,若不重新定位迭代器begin,那么会引发严重的运行时错误。

9.34

由于“++iter;”是while循环后的第一条语句,所以若此容器vi非空,那么程序将陷入无限循环。

9.35

capacity:在不重新分配空间的前提下,容器当前的容量

size:容器当前容纳的元素的数目

9.36

impossible

9.37

list:在内存中不是连续存储的,故不能确定下一个元素存在哪,当然不能事先规定容量(list是链表,当有新元素插入时,会从内存空间分配一个新节点保存它;当从链表中删除元素时,该节点占用的内存空间会被立刻释放。因此一个链表占用的内存空间总是与当前保存的元素所需的空间相等)

array:定义时就包含能容纳的元素的数目且不能改变,故不需要capcity

9.38

 1 #include <iostream>
 2 #include <iterator>
 3 #include <vector> 
 4 #include <string>
 5 #include <deque>
 6 #include <list> 
 7 #include <forward_list>
 8  
 9 using namespace std;
10 
11 int main()
12 { 
13     vector<int> vi;
14     cout << vi.size() << " " << vi.capacity() << endl;
15     
16     vi = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
17     cout << vi.size() << " " << vi.capacity() << endl;
18     
19     vi.reserve(vi.capacity() * 2);
20     cout << vi.capacity() << endl;
21     
22     vi.reserve(10);
23     cout << vi.capacity() << endl;
24     
25     for (vector<int>::size_type i = 0; i != 10; ++i) {
26         vi.push_back(i);
27     }
28     cout << vi.size() << " " << vi.capacity() << endl;
29     
30     vi.push_back(2);
31     cout << vi.size() << " " << vi.capacity() << endl;
32     
33     vi.shrink_to_fit();
34     cout << vi.size() << " " << vi.capacity() << endl;
35     return 0;
36 }
View Code

9.39

首先,reserve为svec分配了1024个元素的空间。随后,循环不断读入字符,添加到svec末尾,直到遇到文件结束符。这个过程中,如果读入的字符串数量不多于1024,则svec的capacity保持不变,不会分配新的内存空间。否则,会按一定规则分配更大的内存空间,并进行字符串的移动。接下来,resize将向svec末尾添加当前字符串数量一半那么多的新字符串,他们的值都是空串。若空间不够,会分配足够容纳这些字符串的内存空间。

9.40

256:1024(size的大小为384)

512:1024(size的大小为768)

1000:1500或更多(我的计算机是每次翻倍增加,故由1000直接变为2000,而size的大小为1500)

1048:1572或更多(我的计算机在while里直接令capacity变为2000,故足以容纳resize之后元素的数目,而size的大小为1572)

9.41

 1 #include <iostream>
 2 #include <iterator>
 3 #include <vector> 
 4 #include <string>
 5 #include <deque>
 6 #include <list> 
 7 #include <forward_list>
 8  
 9 using namespace std;
10  
11 int main()
12 {
13     vector<char> vec = {'l', 'y', '5', '2', '0', 'k', 'z', 'w'};
14     string s(vec.data(), 2);        //vec.data()返回一个指向vec的东东 
15     string s1(vec.data() + 2, vec.size() - 2);
16     cout << s << s1 << endl;
17 }
View Code

9.42

先创建一个空的string对象,然后将其容量reserve为100。

 1 #include <iostream>
 2 #include <iterator>
 3 #include <vector> 
 4 #include <string>
 5 #include <deque>
 6 #include <list> 
 7 #include <forward_list>
 8  
 9 using namespace std;
10  
11 int main()
12 {
13     string s;
14     char c;
15     s.reserve(100);
16     while (cin >> c) {
17         s.push_back(c);
18     }
19 }  
View Code

9.43

 1 #include <iostream>
 2 #include <iterator>
 3 #include <vector> 
 4 #include <string>
 5 #include <deque>
 6 #include <list> 
 7 #include <forward_list>
 8  
 9 using namespace std;
10 
11 void func(string &s, string oldVal, string newVal)
12 {
13     auto it = s.begin();
14     while (it != s.end()) {
15         auto iter = it;
16         auto it1 = oldVal.begin();
17         while (*it1 == *iter && it != s.end() && it1 != oldVal.end()) {
18             ++iter;
19             ++it1;
20         }
21         if (it1 == oldVal.end()) {
22             it = s.erase(it, iter);
23             auto itt = newVal.end() - 1;
24             while (itt != newVal.begin() - 1) {
25                 it = s.insert(it, *itt);
26                 --itt;
27             }
28             it += newVal.size();
29         }
30         else
31             ++it; 
32     }
33 }
34  
35 int main()
36 {
37     string s = {"thot thru wtho"};
38     func(s, "tho", "though");
39     cout << s << endl;
40     
41 }  
View Code

9.44

 1 #include <iostream>
 2 #include <iterator>
 3 #include <vector> 
 4 #include <string>
 5 #include <deque>
 6 #include <list> 
 7 #include <forward_list>
 8  
 9 using namespace std;
10 
11 void func(string &s, string oldVal, string newVal)
12 {
13     int pos = 0;
14     while (pos != s.size()) {
15         auto it = oldVal.begin();
16         while (s[pos] == *it && pos != s.size() && it != oldVal.end()) {
17             ++pos;
18             ++it;
19         }
20         if (it == oldVal.end()) {
21             pos -= oldVal.size();
22             s.replace(pos, oldVal.size(), newVal);        //就这个地方 
23             pos += newVal.size();
24         }
25         else
26             ++pos;
27     }
28 }
29  
30 int main()
31 {
32     string s = {"thot tho thru wtho"};
33     func(s, "tho", "though");
34     cout << s << endl;
35     
36 }  
View Code

9.45

 1 #include <iostream>
 2 #include <iterator>
 3 #include <vector> 
 4 #include <string>
 5 #include <deque>
 6 #include <list> 
 7 #include <forward_list>
 8  
 9 using namespace std;
10 
11 string func(string &s, string sa, string sc)
12 {
13     //等价于s.insert(0, sa); 
14     s.insert(s.begin(), sa.begin(), sa.end());
15     s.append(sc);
16     return s;
17 }
18  
19 int main()
20 {
21     string s = {" Primer "};
22     s = func(s, "C++", "5th");
23     cout << s << endl;
24     
25 }  
View Code

9.46

 1 #include <iostream>
 2 #include <iterator>
 3 #include <vector> 
 4 #include <string>
 5 #include <deque>
 6 #include <list> 
 7 #include <forward_list>
 8  
 9 using namespace std;
10 
11 string func(string &s, string sa, string sc)
12 {
13     s.insert(0, sa); 
14     s.insert(s.size(), sc);        //size()获取数目,比下标多1 
15     return s;
16 }
17  
18 int main()
19 {
20     string s = {" Primer "};
21     s = func(s, "C++", "5th");
22     cout << s << endl;
23 }  
View Code

9.47

 1 #include <iostream>
 2 #include <iterator>
 3 #include <vector> 
 4 #include <string>
 5 #include <deque>
 6 #include <list> 
 7 #include <forward_list>
 8  
 9 using namespace std;
10 
11 void func(string &s, string &s1) 
12 {
13     string::size_type pos = 0;
14     while((pos = s.find_first_of(s1, pos)) != string::npos) {
15         cout << "found number at index: " << pos << " element is " << s[pos] << endl;
16         ++pos;
17     } 
18     cout << endl;
19     pos = 0;
20     while((pos = s.find_first_not_of(s1, pos)) != string::npos) {
21         cout << "found letter at index: " << pos << " element is " << s[pos] << endl;
22         ++pos;
23     }    
24 }
25  
26 int main()
27 {
28     string s = {"ab2c3d7R4E6"};
29     string s1 = {"0123456789"};
30     cout << "Now the string is: " << s << endl;
31     func(s, s1);
32     return 0;
33 }  
View Code

9.48

因为串numbers中不含串name,故搜索失败,返回string::npos。

9.49

 1 #include <iostream>
 2 #include <fstream>
 3 #include <iterator>
 4 #include <vector> 
 5 #include <string>
 6 #include <deque>
 7 #include <list> 
 8 #include <forward_list>
 9  
10 using namespace std;
11  
12 int main()
13 {
14     ifstream in("word.txt");
15     string s = {"a, c, e, m, n, o, r, s, u, v, w, x, z"};
16     string word, maxlenWord;
17     int maxlen = 0;
18     while (in >> word) {
19         //没有找到上/下出头的单词时 
20         if (word.find_first_not_of(s) == string::npos) {
21             if (word.size() > maxlen) {
22                 maxlen = word.size();
23                 maxlenWord = word;
24             }
25         }
26     }
27     if (maxlen)    
28         cout << maxlenWord << endl;
29     else
30         cout << "Don't have this word!
"; 
31     return 0;
32 }  
View Code

9.50

 1 #include <iostream>
 2 #include <fstream>
 3 #include <iterator>
 4 #include <vector> 
 5 #include <string>
 6 #include <deque>
 7 #include <list> 
 8 #include <forward_list>
 9  
10 using namespace std;
11 
12 void func1()
13 {
14     vector<string> vec = {"12", "23", "34"};
15     int sum = 0;
16     for (auto &i : vec) {
17         sum += stoi(i);        //返回int 
18     }
19     cout << sum << endl;
20 }
21 
22 void func2()
23 {
24     vector<string> vec = {"12.5", "23.1", "1.34"};
25     double sum = 0;
26     for (auto &i : vec) {
27         sum += stod(i);        //返回double
28     }
29     cout << sum << endl;
30 }
31 
32 int main()
33 {
34     func1();
35     func2();
36     return 0;
37 }  
View Code

9.51

 1 #include <iostream>
 2 #include <fstream>
 3 #include <iterator>
 4 #include <vector> 
 5 #include <string>
 6 #include <deque>
 7 #include <list> 
 8 #include <forward_list>
 9  
10 using namespace std;
11 
12 class Date{
13     unsigned year;
14     unsigned month;
15     unsigned day;
16     
17 public:
18     Date() {}
19     Date(string date);
20 }; 
21 
22 Date::Date(string date)
23 {
24     vector::<string> mon = {"Jan", "Feb", "Mar", "Apr", "Fri", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
25     string numbers = {"0123456789"};
26     //月份为数字 
27     if (date[0] <= '9' && date[0] >= '0') {
28         if (date[1] <= '9' && date[1] >= '0') {
29             string s1(date, 0, 2);
30             month = stdi(s1);    //返回int,再转为unsigned 
31         }
32         else {
33             string s1(date, 0, 1);
34             month = stdi(s1);
35         }
36         
37     }
38     //月份为字母 
39     else {
40         string s2 = date.substr(0, 3);    //等价于string s2(date, 0, 3) 
41         for (unsigned i = 0; i != mon.size(); ++i) {
42             if (mon[i] == s2) {
43                 month = s2 + 1;
44                 break;
45             }
46         }
47     }
48     
49 }
50 
51 int main()
52 {
53     Date date1("Jan 1 1900");
54     return 0;
55 } 
View Code

//补充

  1 #include<iostream>  
  2 #include<fstream>  
  3 #include<sstream>  
  4 #include<string>  
  5 #include<vector>  
  6 #include<forward_list>  
  7 using namespace std;  
  8   
  9 class Date  
 10 {  
 11 public://class默认是私有继承,记得要加public  
 12     unsigned _year;  
 13     unsigned _month;  
 14     unsigned _day;  
 15     void _show()  
 16     {  
 17         cout<<_year<<""<<_month<<""<<_day<<""<<endl;  
 18     }  
 19     //构造函数  
 20     Date(string);  
 21 };  
 22   
 23 Date::Date(string s)  
 24 {     
 25     int flag = 0;  
 26     string number = "0123456789/";  
 27     string coma = ",";  
 28     string month;     
 29     unsigned pos,pos1,pos2,pos3;      
 30     unsigned _pos,_pos1;  
 31   
 32     /*利用一个判断,现判定怎样初始化*/  
 33     if ((pos = s.find_first_not_of(number)) == string::npos)  
 34     {  
 35         flag = 1;  
 36     }  
 37     if ((pos = s.find_first_of(coma)) != string::npos)  
 38     {  
 39         flag = 2;  
 40     }  
 41   
 42     switch (flag)  
 43     {  
 44     case 1:/*处理1/1/1991的格式*/  
 45         pos1 = 0;  
 46         pos1 = s.find_first_of("/",pos1);  
 47         _day = stoul(s.substr(0,pos1));//先截取目标字符串,再将字符串转化为unsigned  
 48         pos2 = ++pos1;  
 49         pos1 = s.find_first_of("/",pos1);  
 50         _month = stoul(s.substr(pos2,pos1));  
 51         pos3 = ++pos1;  
 52         _year = stoul(s.substr(pos3,s.size()-1));  
 53         break;  
 54     case 2:/*处理January 1,1900的格式*/  
 55         _pos;  
 56         _pos = s.find_first_of(number);  
 57         month = s.substr(0,_pos);  
 58         //本来想用switch,表达式的结果的类型可以是 整数类型,枚举类型,或者类类型  
 59         //(但该类需要有单一的转换到整数类型或(可以是字符类型,但不能是浮点类型、字符串、指针类型等)  
 60         if (month == "January ") _month = 1;  
 61         if (month == "February ") _month = 2;  
 62         if (month == "March ") _month = 3;  
 63         if (month == "April ") _month = 4;  
 64         if (month == "May ") _month = 5;  
 65         if (month == "June ") _month = 6;  
 66         if (month == "July ") _month = 7;  
 67         if (month == "August ") _month = 8;  
 68         if (month == "September ") _month = 9;  
 69         if (month == "October ") _month = 10;  
 70         if (month == "November ") _month = 11;  
 71         if (month == "December ") _month = 12;  
 72   
 73         _pos1 = ++_pos;  
 74         _pos = s.find_first_of(number,_pos);  
 75         _day = stoul(s.substr(_pos1-1,_pos));  
 76   
 77         _year = stoul(s.substr(_pos,s.size()-1));  
 78         break;  
 79     case 0:/*处理Jan 1 1995的格式*/  
 80         _pos;  
 81         _pos = s.find_first_of(number);  
 82         month = s.substr(0,_pos);  
 83         if (month == "Jan ") _month = 1;  
 84         if (month == "Feb ") _month = 2;  
 85         if (month == "Mar ") _month = 3;  
 86         if (month == "Apr ") _month = 4;  
 87         if (month == "May ") _month = 5;  
 88         if (month == "Jun ") _month = 6;  
 89         if (month == "Jul ") _month = 7;  
 90         if (month == "Aug ") _month = 8;  
 91         if (month == "Sep ") _month = 9;  
 92         if (month == "Oct ") _month = 10;  
 93         if (month == "Nov ") _month = 11;  
 94         if (month == "Dec ") _month = 12;  
 95   
 96         _pos1 = ++_pos;  
 97         _pos = s.find_first_of(number,_pos);  
 98         _day = stoul(s.substr(_pos1-1,_pos));  
 99   
100         _year = stoul(s.substr(_pos,s.size()-1));  
101         break;  
102     }     
103 }  
104   
105 int main(int argc, char**argv)  
106 {  
107     Date _today("25/2/2017");  
108     _today._show();  
109   
110     Date _tomorrow("January 1,1995");  
111     _tomorrow._show();  
112   
113     Date _2tomorrow("Jan 1 1995");  
114     _2tomorrow._show();  
115       
116     return 0;  
117 } 
View Code

9.52

 1 #include <iostream>
 2 #include <fstream>
 3 #include <sstream>
 4 #include <iterator>
 5 #include <vector> 
 6 #include <string>
 7 #include <deque>
 8 #include <list> 
 9 #include <forward_list>
10 #include <stack>
11 #include <queue> 
12  
13 using namespace std;
14 
15 
16 int main()
17 {
18     string s = "((4-2)*3)/2";
19     stack<char> sta, sta2;
20     istringstream in(s);
21     char c;
22     while(in >> c) {
23         if (c != ')') {
24             sta.push(c);
25         }
26         else {
27             sta.pop();
28             char ch = sta.top();
29             while (ch != '(') {
30                 sta2.push(ch);
31                 sta.pop();
32                 ch = sta.top();
33             }
34             sta.pop();        //弹出(
35             int sum = 表达式的值;
36             sta.push(sum); 
37         }
38     }
39     return 0;
40 }  
View Code

 

原文地址:https://www.cnblogs.com/xzxl/p/7687408.html