cb06a_c++_顺序容器的定义

/*cb06a_c++_顺序容器的定义
顺序容器:
vector,数组,尾端操作数据,快速随机访问
list 链表,快速插入数据
deque数组,双端-首尾操作数据,方便两端的数据访问

顺序容器适配器:
stack
queue
priority_queue
一种容器的数据初始化另一种类型容器
//用迭代器,vector容器初始化list.
list<string> slist(svec.begin(),svec.end());
关联容器:xxx

字符串数组:
char str1[]="abc";
char str2[]="def";
char str3[]="ghi";
char str4[]="jkl";
char str5[]="xyz";
char *strArray[5]={str1,str2,str3,str4,str5};
或者:
const char *words[] = { "stately","plump1","buck","mulligan" };//字符串数组
size_t words_size = sizeof(words) / sizeof(char *);//统计整体占多少字节,除以每个字符串占得字节数,得到有多少个字符串
error C2248: “Dog::Dog”: 无法访问 private 成员(在“Dog”类中声明),dog class没有写public,默认是private

 1 /*cb06a_c++_顺序容器的定义
 2 顺序容器:
 3 vector,数组,尾端操作数据,快速随机访问
 4 list 链表,快速插入数据
 5 deque数组,双端-首尾操作数据,方便两端的数据访问
 6 
 7 顺序容器适配器:
 8 stack
 9 queue
10 priority_queue
11   一种容器的数据初始化另一种类型容器
12  //用迭代器,vector容器初始化list.
13     list<string> slist(svec.begin(),svec.end());
14 关联容器:xxx
15 
16 字符串数组:
17 char str1[]="abc";
18 char str2[]="def";
19 char str3[]="ghi";
20 char str4[]="jkl";
21 char str5[]="xyz";
22 char *strArray[5]={str1,str2,str3,str4,str5};
23 或者:
24 const char  *words[] = { "stately","plump1","buck","mulligan" };//字符串数组
25 size_t words_size = sizeof(words) / sizeof(char *);//统计整体占多少字节,除以每个字符串占得字节数,得到有多少个字符串
26 error C2248: “Dog::Dog”: 无法访问 private 成员(在“Dog”类中声明),dog class没有写public,默认是private
27 */
28 #include <iostream>
29 #include <vector>
30 #include <list>
31 #include <deque>
32 #include <string>
33 #include "Cat.h"
34 #include "Dog.h"
35 
36 using namespace std;
37 
38 int main()
39 {
40     vector<string> svec; //调用默认的构造函数创建的。
41     svec.push_back("hello");
42     svec.push_back("C++");
43     svec.push_back("STL");
44     list<int> ilist;
45     vector<Dog> dogvec;//dogvec是空的向量。不会调用默认构造函数
46     vector<Dog> b(10);//b有10个对象,都是Dog的默认构造函数初始化
47     vector<Dog> b1(10,2);//调用dog 类中有参数的构造函数
48     list<Cat> catlist;
49     //deque<Sales_item> items;
50 
51     vector<int> ivec;
52     ivec.push_back(1);
53     ivec.push_back(2);
54     ivec.push_back(3);
55     vector<int> ivec2(ivec);//ivec2=ivec;
56 
57     //list<int> ilist(ivec);//错误,vector与list类型不匹配
58      //用迭代器,vector容器初始化list.
59     list<string> slist(svec.begin(),svec.end());
60     vector<string>::iterator mid = svec.begin() + svec.size() / 2;//指向了数据元素的中间
61     deque<string> front(svec.begin(), mid);//svec前一半的数据初始化front;
62     deque<string> back(mid, svec.end());//用后一半的数据去初始化
63     //char  *words = {"stately","plump1","buck","mulligan"};
64     //error C2440: “初始化”: 无法从“initializer list”转换为“char *”
65     //*words改为*words[];
66     //error C2440 : “初始化”: 无法从“const char[8]”转换为“char *”,加上const就可以了
67      const char  *words[] = { "stately","plump1","buck","mulligan" };//字符串数组,数组指针
68      size_t words_size = sizeof(words) / sizeof(char *);
69      
70 
71      list<string> words1(words, words + words_size);//char数据放入list.  //words1={"stately","plump1","buck","mulligan"};
72      
73      const list<int>::size_type list_size = 64;
74      //list<string> slist(64);//64个空串。
75      list<string> slist1a(list_size,"hello");//64个hello.
76      list<int> ilist2a(list_size, 8);//64个8;
77 
78      //容器里面放容器
79      vector< vector<string> >lines;//里面的vector记得空格隔开。
80      list< vector<string> > slist22;
81 
82      //梁平柚
83      //颂和时代城
84 
85     return 0;
86 }

 

posted @ 2020-02-14 20:44  txwtech  阅读(248)  评论(0编辑  收藏  举报