STL 之vector string


1、vector容器,可以理解為數組,可以理解為單端數組,可以動態擴展(重新開辟一片更大空間,把原有的數據再拷貝進去,釋放原來的空間)既可以存放內置數據類型,又可以存放自定義數據類型。

  (1)vector存放內置數據類型

 1 #include<iostream>
 2 #include<vector>
 3 #include<algorithm>
 4 using  namespace std;
 5 void print(int val){
 6     cout << val << " ";
 7 }
 8 void test01(){
 9     vector<int> v;  //存放內置數據類型  無參構造  vector<int> v1(v.begin(),v.end());
10     //向容器插入元素
11     v.push_back(1);
12     v.push_back(2);
13     v.push_back(3);
14     //第一種遍歷方式
15     vector<int>::iterator itBegin = v.begin(); //起始迭代器 指向容器中的第一個位置
16     vector<int>::iterator itEnd = v.end();//指向容器中最后一個元素的下一個位置
17     while (itBegin!=itEnd)
18     {
19         cout << *itBegin << " ";
20         itBegin++;
21     }
22     cout << endl;
23     //第二種
24     for (vector<int>::iterator it = v.begin(); it != v.end(); it++){
25         cout << *it << " ";
26     }
27     //第三種 stl提供的算法
28     for_each(v.begin(), v.end() , print);
29 
30 }
31 int main(){
32     test01();
33     system("pause");
34 }

  上面中例子使用的是vector的默認構造,也就是無參構造,還有三種構造函數,分別區間構造、n個elem構造、拷貝構造,例如:

1 vector<int> v1(v.begin(),v.end()); //區間方式構造
2 
3 vector<int > v2(10,1);  //v2里面有10個1
4 
5 vector<int > v3(v2)  //拷貝構造

  (2)vector存放自定義數據類型

 1 #include<iostream>
 2 #include<vector>
 3 #include<string>
 4 #include<algorithm>
 5 using namespace std;
 6 class Person{
 7 public:
 8     Person(string name,int age){
 9         this->m_Name = name;
10         this->m_Age = age;
11     }
12     string m_Name;
13     int m_Age;
14 };
15 void print(Person p){
16     cout << p.m_Name << " ";
17     cout << p.m_Age << endl;
18 }
19 void test01(){
20     vector<Person>v;
21     Person p1("aa", 12);
22     Person p2("bb", 13);
23     Person p3("cc", 14);
24     //向容器中插入數據
25     v.push_back(p1);
26     v.push_back(p2);
27     v.push_back(p3);
28     
29     //遍歷
30     for_each(v.begin(), v.end(), print);
31     for (vector<Person>::iterator it = v.begin();it != v.end(); it++){ //it指針
32         cout << it->m_Name << " " << it->m_Age << endl;
33     }
34 }
35 void print1(Person *p){
36     cout << (p)->m_Age << " " << p->m_Name << endl;
37 }
38 void test02(){
39     vector<Person *> v1;
40     Person p1("tom", 18);
41     Person p2("tom", 19);
42     v1.push_back(&p1);
43     v1.push_back(&p2);
44     for_each(v1.begin(), v1.end(), print1);
45     for (vector<Person*>::iterator it = v1.begin(); it != v1.end(); it++){
46         cout << (*it)->m_Name << " " << (*it)->m_Age << endl;
47     }
48 }
49 int main(){
50     /*test01();*/
51     test02();
52     system("pause");
53 }

  (3)vector容器嵌套

 1 #include<iostream>
 2 #include<vector>
 3 #include<string>
 4 using namespace std;
 5 void test01(){
 6     vector<vector<int>>v;
 7     //創建小容器
 8     vector<int>v1;
 9     vector<int>v2;
10     vector<int>v3;
11     //往小容器中插入數據
12     for (int i = 0; i < 3; i++)
13     {
14         v1.push_back(i + 1);
15         v2.push_back(i + 2);
16         v3.push_back(i + 3);
17     }
18     //在大容器中插入小容器
19     v.push_back(v1);
20     v.push_back(v2);
21     v.push_back(v3);
22     //遍歷
23     for (vector<vector<int>>::iterator it = v.begin(); it != v.end();it++)
24     {
25         for (vector<int>::iterator it1 = (*it).begin(); it1!= (*it).end();it1++)
26         {
27             cout << *it1 << " ";
28         }
29         cout << endl;
30     }
31 
32 }
33 int main(){
34     test01();
35     system("pause");
36 }

(4)vector賦值操作

 1 //1、operator=賦值
 2 //把v1里面的值賦值給v2
 3 vector<int> v2;  //默認構造
 4 v2=v1;
 5 
 6 //2、assign區間賦值
 7 vector<int> v2 ;  //默認構造
 8 v2.assign(v1.begin(),v2.end());  //區間開閉后開
 9 
10 //3、assign n個元素的方式賦值
11 vector<int > v2 ;  //默認構造
12 v2.assign(10,2);

(5)vector的容量和大小

 1     vector<int>v1;
 2     v1.push_back(1);
 3     v1.push_back(2);
 4     v1.push_back(3);
 5     v1.push_back(4);
 6 
 7     if (v1.empty())
 8     {
 9         cout << "v1為空" << endl;
10     }
11     //v1.resize(12);//重新指定大小,超出部分默認用0擴充
12     v1.resize(12, 1); //指定用1擴充
13     int cap=v1.capacity();  //容器容量
14     
15     cout << "v1的容量" << cap << endl;  //容量永遠大於或者等於size()
16     int size = v1.size();  //容器大小
17     cout << "size=" << size << endl;

(6)vector的插入和刪除

1 vector<int>v1;
2 for (int i = 0; i < 10; i++)
3 {
4     v1.push_back(i);   //尾插法
5 }
6v1.insert(v1.begin(),2, 12);  //通過迭代器插入數據,在起始位置,插入兩個12
7 v1.erase(v1.begin());//刪除第一個元素
8 v1.erase(v1.begin(), v1.end()); //刪除區間元素相當於清空
9 v1.clear();//清空

(7)vector數據存取

 1     vector<int>v1;
 2     for (int i = 0; i < 10; i++)
 3     {
 4         v1.push_back(i);   //尾插法
 5     }
 6     //通過[]訪問
 7     for (int i = 0; i < v1.size(); i++)
 8     {
 9         cout << v1[i] << " ";
10     }
11     cout << endl;
12     //通過at方式訪問
13     for (int i = 0; i < v1.size(); i++)
14     {
15         cout << v1.at(i) << " ";
16     }
17     cout << endl;
18     cout<<"第一個元素是:"<<v1.front()<<endl;
19     cout<<"最后一個元素:"<<v1.back()<<endl;

 

 

2、string容器,內部封裝了char*,管理字符串,是一個char*型的容器。

(1)string的構造函數;

    

 1 //空構造
 2 string();
 3 string s1;
 4 //使用字符串s初始化
 5 string(const char*s);
 6 const char* s="hi";
 7 string s2(s);
 8 //使用string對象初始化另一個string對象,拷貝構造
 9 string(const string&s);
10 string s3(s2);
11 //n個字符
12 string (int n,char c);
13 string s4(3,'d');

(2)string賦值操作

 1 const char *p = "hello world";
 2     string s1;
 3     s1 = p;   //s1="hello world"
 4     cout << "s1=" << s1 << endl;
 5 
 6     string s2 = s1;
 7     cout << "s2=" << s2 << endl;
 8 
 9     string s4;
10     s4.assign("hello world");
11     cout << "s4=" << s4 << endl;
12 
13     string s5;
14     s5.assign("hello world", 5);
15     cout << "s5=" << s5 << endl;
16 
17     string s6;
18     s6.assign(s5);
19     cout << "s6=" << s6 << endl;
20 
21     string s7;
22     s7.assign(10, 'a');
23     cout << "s7=" << s7 << endl;

(3)string拼接

  

 1 //1、+=方式
 2     string str1 = "I like ";
 3     str1 += "cat";
 4     cout << "str1=" << str1 << endl;
 5 
 6     str1 += ':';
 7     cout << "str1=" << str1 << endl;
 8 
 9     string str2 = "tom ";
10     str1 += str2;
11     cout << "str1=" << str1 << endl;
12 
13     //2、append方式
14     str1.append("miaomiao");
15     cout << "str1=" << str1 << endl;
16 
17     str1.append("miaowu", 4);
18     cout << "str1=" << str1 << endl;
19 
20     str1.append(str2);
21     cout << "str1=" << str1 << endl;
22 
23     str1.append(str2, 0, 2);  //從0號位置開始截取兩位
24     cout << "str1=" << str1 << endl;

(4)string查找和替換

1     string str1 = "abcdefde";
2     int pos=str1.find("de");
3     cout << "pos=" << pos << endl;   //3  find從左往右查找
4     pos = str1.rfind("de");
5     cout << "pos=" << pos << endl;   //6  rfind從右往左找
1     string str1 = "absdf";
2     str1.replace(0, 2, "000");  //從0號位置起2個字符替換為000
3     cout << "str1=" << str1 << endl;    //000sdf

(5)字符串比較

1 string str1="hello";
2 string str2 = "hello";
3 int ret =str1.compare(str2);
4 if(ret==0)
5 {
6    //字符串相同  
7 }

(6)字符存取,通過“[]”或者“at”方式對字符串進行讀和寫。

1     string str1 = "hello";
2     for (int i = 0; i < str1.size(); i++)
3     {
4         //cout << str1[i] << " ";    //通過[]的方式訪問
5         cout << str1.at(i) << " ";   //通過at的方式訪問
6     }
1     str1[0] = 'x';
2     str1.at(1) = 'x';
3     cout << "str1=" << str1 << endl;    //hello-->xxllo

(7)string 的插入和刪除

1 string str="hello";
2 str.insert(0,"aaa");   //在第0個位置之前插入aaa  hello -->aaahello
3 str.erase(0,3);  //從0位置起刪除3個  aaahello--->hello

(8)從字符串種獲取想要的字串

1     string str1 = "abcdef";
2     string substr = str1.substr(1, 3);   //從1號位置截取3個
3     cout << "substr=" << substr << endl;
4     //實用操作  ,從郵箱中獲取name
5     string str2 = "liming@sina.com";
6     int pos = str2.find('@');   //pos=6
7     string name = str2.substr(0, pos);
8     cout << "name=" << name << endl;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM