主要内容 :string
vector
list
动机 刷leetcode的时候感觉这一块太重要了,还是补一补吧。
注意可以抄这篇:https://blog.csdn.net/q5120192609/article/details/127587508
string 构造 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include <iostream> using namespace std;void test01 () { const char * str = "hello world" ; string s1; string s2 (str) ; string s3 (s2) ; string s4 (10 , 'a' ) ; cout << "s2 = " << s2 << endl; cout << "s3 = " << s3 << endl; cout << "s4 = " << s4 << endl; }
赋值 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 #include <iostream> using namespace std;void test01 () { string s1; s1 = "hello world" ; cout << s1 << endl; string s2 = s1; cout << s2 << endl; string s3; s3 = 'c' ; cout << s3 << endl; string s4; s4.assign ("hello C++" ); cout << s4 << endl; string s5; s5.assign ("hello C++" , 5 ); cout << s5 << endl; string s6; s6.assign (s5); cout << s6 << endl; string s7; s7.assign (5 , 'a' ); }
拼接 主要是 +=
和 append
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include <iostream> using namespace std;void test01 () { string str1 = "我" ; str1 += "不爱玩游戏,爱学习" ; cout << str1 << endl; string str3 = "I" ; str3.append ("love" ); cout << str3 << endl; string str4 = "learning aaa" ; str3.append (str4, 0 , 8 ); cout << str3 << endl; }
查找与替换 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include <iostream> using namespace std;void test01 () { string str1 = "abcdefgde" ; int pos1 = str1.find ("de" ); int pos2 = str1.find ("df" ); cout << pos1 << endl; cout << pos2 << endl; int pos3 = str1.rfind ("de" ); cout << pos3 << endl; } void test02 () { string str1 = "Abcdefg" ; str1.replace (1 , 3 , "1111" ); cout << str1 << endl; }
比较 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 void test01 () { string str1 = "hello" ; string str2 = "hello" ; if (str1.compare (str2) == 0 ) { cout << "str1 = str2" << endl; } else if (str1.compare (str2) > 0 ) { cout << "str1 > str2" << endl; } else { cout << "str1 < str2" << endl; } }
vector vector即数组
容器:vector 算法:for_each 迭代器:vector<int>::iterator
sort(L1.begin(), L1.end())
list 反转: L1.reverse()
排序: L1.sort()
所有不支持随机访问迭代器的容器不可以使用标准算法;不支持随机访问迭代器的容器内部会提供成员函数
另外,可以通过回调函数设置排序依据,下面是一个设置降序的例子:
1 2 3 4 5 6 7 8 9 L1.sort (); bool myCompare (int v1, int v2) { return v1 > v2; } L1.sort (myCompare)
下面是一个设置自定义类的例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 #include <iostream> #include <string> #include <list> #include <algorithm> using namespace std;class Person {public : Person (string name,int age,int height){ m_name = name; m_age = age; m_height = height; } string m_name; int m_age; int m_height; }; bool compare (Person &p1,Person &p2) { if (p1.m_age == p2.m_age) { return p1.m_height>p2.m_height; } else { return p1.m_age<p2.m_age; } } int main () { Person p1 ("刘备" ,35 ,175 ) ; Person p2 ("曹操" ,45 ,188 ) ; Person p3 ("孙权" ,48 ,170 ) ; Person p4 ("赵云" ,25 ,198 ) ; Person p5 ("张飞" ,35 ,160 ) ; Person p6 ("关羽" ,35 ,200 ) ; list<Person> myL; myL.push_back (p1); myL.push_back (p2); myL.push_back (p3); myL.push_back (p4); myL.push_back (p5); myL.push_back (p6); for (list<Person>::iterator it = myL.begin ();it != myL.end ();it++){ cout<<"姓名: " <<it->m_name<<" 年龄 :" <<it->m_age<<" 身高 :" <<it->m_height<<endl; } cout << "-----------------" << endl; myL.sort (compare); for (list<Person>::iterator it = myL.begin ();it != myL.end ();it++){ cout<<"姓名: " <<it->m_name<<" 年龄 :" <<it->m_age<<" 身高 :" <<it->m_height<<endl; } return 0 ; }