<大纲> 基本概念 vector数组基本数据类型操作 vector数组复杂数据类型操作 vector数组指针数据类型操作 string 字符串的初始化: string的 遍历之 for string的 遍历之迭代器 string的 遍历 之 at() string的 遍历 [] string的 遍历 [] 与 at()对比 string指针与string转换 : s1===>char * string指针与string转换 : char *====>sting string指针与string转换 : s1的内容copy buf中 string字符串的连接 string字符串的查找 find()返回第一次出现的位置 string查找字符串出现的次数: string字符替换:replace() string字符串的查找并替换 string单个字符的删除 string删除整个字符串 string字符串的头插法,尾插法 string字符串大小写转换 容器 vector容器 vector容器元素数量,尾部插入,头部元素,尾部元素,尾部元素删除 vector容器的初始化: vector的遍历 通过数组的方式 vectorpush_back强化训练 vector区间删除: vector删除指定位置的元素: vector根据元素的值,删除元素 vector在首部,尾部插入元素: 迭代器,正向反向 deque双端数组容器:尾插,头插,遍历,首部弹出,尾部弹出 deque双端数组容器:查找元素所在容器的位置 stack容器:入栈,出栈,栈顶,栈大小 stack装入类 stack装入指针 queue队列:基本元素 push() front() size() pop() queue队列:复杂类型 queue队列:指针类型 list的基本操作,元素的插入,遍历,不支持随机访问 list的删除,移除, priority_queue优先级队列 set set的基本操作insert,迭代器,erase set的基本操作2:插入,遍历,删除 set对基本数据类型,能自动排序: set2 集合从大到小 set复杂数据类型 自定义数据类型,仿函数排序法 如何判断 set1.insert函数的返回值 find查找 equal_range,返回结果是一个pair set集合的查找: multiset基本操练:
===================================================================
容器的基本数据操作
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> using namespace std; //STL #include <vector> #include <algorithm> int main() { vector<int> v1; //容器,把元素拷贝到容器 v1.push_back(999); v1.push_back(1); v1.push_back(-55654); v1.push_back(999); //迭代器,相当于一个指针 for(vector<int>::iterator i = v1.begin();i != v1.end();i++) { cout << *i << " "; } cout << endl; //算法结合迭代器,找出从 v1.begin()到v1.end()有多少个999 int n = count(v1.begin(),v1.end(),999); cout << n << endl; return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run 999 1 -55654 999 2 chunli@http://990487026.blog.51cto.com~/c++$
容器里面装复杂数据类型
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> using namespace std; //STL #include <vector> #include <algorithm> class Teacher { public: int age; char name[64]; void printf() { cout << "age="<< age<<" name=" << name<<endl; } }; int main() { Teacher t1; t1.age = 11; Teacher t2; t2.age = 12; Teacher t3; t3.age = 13; Teacher t4; t4.age = 14; vector<Teacher> v1; //容器,把元素拷贝到容器 v1.push_back(t1); v1.push_back(t2); v1.push_back(t3); v1.push_back(t4); //迭代器,相当于一个指针 for(vector<Teacher>::iterator i = v1.begin();i != v1.end();i++) { cout << i->age << " "; //cout << (*i).age << " "; } cout << endl; return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run 11 12 13 14 chunli@http://990487026.blog.51cto.com~/c++$
把指针装入容器
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> using namespace std; //STL #include <vector> #include <algorithm> class Teacher { public: int age; char name[64]; void printf() { cout << "age="<< age<<" name=" << name<<endl; } }; int main() { Teacher t1; t1.age = 11; Teacher t2; t2.age = 12; Teacher t3; t3.age = 13; Teacher t4; t4.age = 14; Teacher *p1 = &t1; Teacher *p2 = &t2; Teacher *p3 = &t3; Teacher *p4 = &t4; vector<Teacher*> v1; //容器,把地址拷贝到容器 v1.push_back(p1); v1.push_back(p2); v1.push_back(p3); v1.push_back(p4); //迭代器,相当于一个指针 for(vector<Teacher*>::iterator i = v1.begin();i != v1.end();i++) { cout << (*i)->age << " "; //cout << (*i).age << " "; } cout << endl; return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run 11 12 13 14 chunli@http://990487026.blog.51cto.com~/c++$
STL Srring
1 字符串的初始化:
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> using namespace std; //STL #include <vector> #include <string> #include <algorithm> int main() { string s1 = "aaaa"; string s2("bbbb"); string s3 = s2; //通过拷贝构造函数 来初始化对象s3 string s4(10, 'a'); cout << "s1=" << s1 << endl; cout << "s2=" << s2 << endl; cout << "s3=" << s3 << endl; cout << "s4=" << s4 << endl; return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run s1=aaaa s2=bbbb s3=bbbb s4=aaaaaaaaaa chunli@http://990487026.blog.51cto.com~/c++$ ================================================ string的 遍历1 [] chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> using namespace std; //STL #include <vector> #include <string> #include <algorithm> int main() { string s1 = "abcdefg"; //1 数组方式 for (int i=0; i<s1.length(); i++) { cout << s1[i] << " "; } cout << endl; return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run a b c d e f g chunli@http://990487026.blog.51cto.com~/c++$
string的 遍历2 迭代器
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> using namespace std; //STL #include <vector> #include <string> #include <algorithm> int main() { string s1 = "abcdefg"; //2 迭代器 for (string::iterator it = s1.begin(); it != s1.end(); it++ ) { cout << *it << " "; } cout << endl; return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run a b c d e f g chunli@http://990487026.blog.51cto.com~/c++$
string的 遍历2 at(),可以捕获异常
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> using namespace std; //STL #include <vector> #include <string> #include <algorithm> int main() { string s1 = "abcdefg"; try { for (int i=0; i<s1.length() + 3; i++) { cout << s1.at(i) << " "; //抛出异常 } } catch ( ... ) { cout << "发生异常\n" ; } return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run a b c d e f g 发生异常 chunli@http://990487026.blog.51cto.com~/c++$
string的 遍历 [] 与 at()对比,[]不会捕获异常,VS环境下程序会中断,GCC下就不会
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> using namespace std; //STL #include <vector> #include <string> #include <algorithm> int main() { string s1 = "abcdefg"; try { for (int i=0; i<s1.length() + 3; i++) { cout << s1[i] << " "; //出现错误 不向外面抛出异常 引起程序的中断 } cout << endl; } catch ( ... ) { cout << "发生异常\n" ; } return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run a b c d e f g chunli@http://990487026.blog.51cto.com~/c++$ GDB调试。程序是正常退出的 [Inferior 1 (process 1605) exited normally] (gdb) quit
2 字符串,指针与string转换 : s1===>char *
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> using namespace std; //STL #include <vector> #include <string> #include <algorithm> int main() { string s1 = "aaabbbb"; //1 s1===>char * const char *p = s1.c_str() ; cout << p << endl; return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run aaabbbb chunli@http://990487026.blog.51cto.com~/c++$
2 字符串,指针与string转换 : char *====>sting
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> using namespace std; //STL #include <vector> #include <string> #include <algorithm> int main() { string s1 = "aaabbbb"; //1 s1===>char * const char *p = s1.c_str() ; //2 char *====>sting string s2 = p; cout << s2 << endl;; return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run aaabbbb chunli@http://990487026.blog.51cto.com~/c++$
2 字符串,指针与string转换 : s1的内容 copy buf中
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> using namespace std; //STL #include <vector> #include <string> #include <algorithm> int main() { string s1 = "aaabbbb"; //3 s1的内容 copy buf中 char buf1[128] = {0}; s1.copy(buf1, 3, 0); //注意 只给你copy3个字符 不会变成C风格的字符串 cout << buf1 << endl; return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run aaa chunli@http://990487026.blog.51cto.com~/c++$
3 string 字符串的连接
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> using namespace std; //STL #include <vector> #include <string> #include <algorithm> int main() { string s1 = "aaa"; string s2 = "bbb"; s1 = s1 + s2; cout << s1 << endl; string s3 = "333"; string s4 = "444"; s3.append(s4); cout << s3 << endl; return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run aaabbb 333444 chunli@http://990487026.blog.51cto.com~/c++$
4 string 字符串的查找 find()返回第一次出现的位置
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> using namespace std; //STL #include <vector> #include <string> #include <algorithm> int main() { string s1 = "linux hello linux 111 linux 222 linux 333 "; int index = s1.find("hello", 0); //位置下标 从0开始 cout << "index: " << index << endl; return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run index: 6 chunli@http://990487026.blog.51cto.com~/c++$
4 查找字符串出现的次数:
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> using namespace std; //STL #include <vector> #include <string> #include <algorithm> int main() { string s1 = " linux hello linux 111 linux 222 linux 333 "; int offindex = s1.find("linux", 0); while (offindex != string::npos) //这里的string::npos就是一个长度参数,表示直到字符串的结束 { cout << "位置在 " << offindex << endl; offindex = offindex + 1; offindex = s1.find("linux", offindex); //wang bao ming } return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run 位置在 1 位置在 13 位置在 24 位置在 35 chunli@http://990487026.blog.51cto.com~/c++$
4 string 字符替换:replace()
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> using namespace std; //STL #include <vector> #include <string> #include <algorithm> int main() { string s3 = "aaa bbb ccc"; s3.replace(0, 3, "AAA"); cout << s3 << endl; return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run AAA bbb ccc chunli@http://990487026.blog.51cto.com~/c++$
4 string 字符串的查找并替换:
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> using namespace std; //STL #include <vector> #include <string> #include <string.h> #include <algorithm> int main() { string s1 = " linux hello linux 111 linux 222 linux 333 "; int offindex = s1.find("linux", 0); while (offindex != string::npos) { cout << "位置" << offindex << endl; //显示字符串找到的位置 s1.replace(offindex,strlen("linux"), "China_linux"); //替换成你想要的字符串 offindex = offindex + strlen("China_linux"); //位置偏移量, offindex = s1.find("linux", offindex); //继续找 } cout << "s1替换后的结果: " << s1 << endl; return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run 位置1 位置19 位置36 位置53 s1替换后的结果: China_linux hello China_linux 111 China_linux 222 China_linux 333 chunli@http://990487026.blog.51cto.com~/c++$
string 单个字符的删除
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> using namespace std; //STL #include <vector> #include <string> #include <string.h> #include <algorithm> int main() { string s1 = "hello1 hello2 hello1"; string::iterator it = find(s1.begin(), s1.end(), 'e'); if (it != s1.end() ) { s1.erase(it); } cout<< s1 << endl; return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run hllo1 hello2 hello1 chunli@http://990487026.blog.51cto.com~/c++$
4 string 删除整个字符串
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> using namespace std; //STL #include <vector> #include <string> #include <string.h> #include <algorithm> int main() { string s1 = "hello1 hello2 hello1"; s1.erase(s1.begin(), s1.end() ); cout << "s1全部删除:" << s1 << endl; cout << "s1长度 " << s1.length() << endl; return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run s1全部删除: s1长度 0 chunli@http://990487026.blog.51cto.com~/c++$
4 string 字符串的头插法,尾插法
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> using namespace std; //STL #include <vector> #include <string> #include <string.h> #include <algorithm> int main() { string s2 = "BBB"; s2.insert(0, "AAA "); //头插法 s2.insert(s2.length(), " CCC"); //尾插法 cout << s2 << endl; return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run AAA BBB CCC chunli@http://990487026.blog.51cto.com~/c++$
5 string字符串大小写转换
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> using namespace std; //STL #include <string> #include <algorithm> int main() { string s1 = "AAAbbb"; //1字符串开始,2字符串结束,3放到那里去,4 怎么放 transform(s1.begin(), s1.end(),s1.begin(),::toupper); cout << s1 << endl; transform(s1.begin(), s1.end(), s1.begin(),::tolower); cout << s1 << endl; return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run AAABBB aaabbb chunli@http://990487026.blog.51cto.com~/c++$
容器
vector容器:
容器元素数量,尾部插入,头部元素,尾部元素,尾部元素删除
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> using namespace std; #include <vector> int main() { vector<int> v1; cout << "length:" << v1.size() <<endl; //显示容器里面的数目 v1.push_back(1); //在尾部插入int元素 1 v1.push_back(3); v1.push_back(5); cout << "length:" << v1.size() <<endl; cout << "头部元素是" << v1.front() << endl;//front()是头部元素 v1.front() = 11; //修改 头部元素 函数返回值当左值 应该返回一个引用 v1.back() = 99; //修改 尾部元素 函数返回值当左值 应该返回一个引用 while (v1.size() > 0) //遍历容器 { cout <<"尾部元素是" << v1.back() << endl; ; //获取尾部元素 v1.pop_back(); //删除尾部元素 } return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run length:0 length:3 头部元素是1 尾部元素是99 尾部元素是3 尾部元素是11 chunli@http://990487026.blog.51cto.com~/c++$
vector容器的初始化:
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> using namespace std; #include <vector> void printV(vector<int> &v) { for (int i=0; i<v.size(); i++) { cout << v[i] << " "; } cout << endl; } int main() { vector<int> v1; v1.push_back(1); v1.push_back(3); v1.push_back(5); v1.push_back(7); vector<int> v2 = v1; //对象初始化 vector<int> v3(v1.begin()+1, v1.begin()+3 ); //初始化来自地址 printV(v3); return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run 3 5 chunli@http://990487026.blog.51cto.com~/c++$
vector的遍历 通过数组的方式
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> using namespace std; #include <vector> int main() { vector<int> v1(10); //提前把10个内存空间准备好 for (int i=0; i<10; i++) { v1[i] = i * i; } for (int i=0; i<10; i++) { cout << v1[i] << " "; } cout << endl; return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run 0 1 4 9 16 25 36 49 64 81 chunli@http://990487026.blog.51cto.com~/c++$
vector push_back强化训练
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> using namespace std; #include <vector> void printV(vector<int> &v) { for (int i=0; i<v.size(); i++) { cout << v[i] << " "; } cout << endl; } int main() { vector<int> v1(10); //提前把内存准备好 v1.push_back(100); v1.push_back(200); cout << "size: " << v1.size() << endl; printV(v1); return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run size: 12 0 0 0 0 0 0 0 0 0 0 100 200 chunli@http://990487026.blog.51cto.com~/c++$
迭代器,正向 反向
//1迭代器 end()的理解 // 1 3 5 // ▲ // ▲ //当 it == v1.end()的时候 说明这个容器已经遍历完毕了... //end()的位置 应该是 5的后面 //2 迭代器的种类 /* typedef iterator pointer; typedef const_iterator const_pointer; typedef _STD reverse_iterator<iterator> reverse_iterator; typedef _STD reverse_iterator<const_iterator> const_reverse_iterator; */ chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> using namespace std; #include <vector> int main() { vector<int> v1(10); for (int i=0; i<10; i++) { v1[i] = i + 1; } //正向遍历 for (vector<int>::iterator it = v1.begin(); it != v1.end(); it ++ ) { cout << *it << " "; } cout << endl; //逆序遍历 for (vector<int>::reverse_iterator rit = v1.rbegin(); rit!=v1.rend(); rit++ ) { cout << *rit << " "; } cout << endl; return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run 1 2 3 4 5 6 7 8 9 10 10 9 8 7 6 5 4 3 2 1 chunli@http://990487026.blog.51cto.com~/c++$
vector区间删除:
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> using namespace std; #include <vector> void printV(vector<int> &v) { for (int i=0; i<v.size(); i++) { cout << v[i] << " "; } cout << endl; } int main() { vector<int> v1(10); for (int i=0; i<10; i++) { v1[i] = i + 1; } printV(v1); v1.erase(v1.begin(), v1.begin()+3); //区间删除 printV(v1); return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run 1 2 3 4 5 6 7 8 9 10 4 5 6 7 8 9 10 chunli@http://990487026.blog.51cto.com~/c++$ chunli@http://990487026.blog.51cto.com~/c++$
vector 删除指定位置的元素:
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> using namespace std; #include <vector> void printV(vector<int> &v) { for (int i=0; i<v.size(); i++) { cout << v[i] << " "; } cout << endl; } int main() { vector<int> v1(10); for (int i=0; i<10; i++) { v1[i] = i + 1; } printV(v1); //根据元素的位置 指定位置删除 v1.erase(v1.begin()); //在头部删除一个元素 printV(v1); return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run 1 2 3 4 5 6 7 8 9 10 2 3 4 5 6 7 8 9 10 chunli@http://990487026.blog.51cto.com~/c++$
vector 根据元素的值,删除元素
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> using namespace std; #include <vector> void printV(vector<int> &v) { for (int i=0; i<v.size(); i++) { cout << v[i] << " "; } cout << endl; } int main() { vector<int> v1(10); for (int i=0; i<10; i++) { v1[i] = i + 1; } //根据元素的值 v1[1] = 2; v1[3] = 2; printV(v1); for (vector<int>::iterator it =v1.begin(); it != v1.end();) { if (*it == 2) { it = v1.erase(it); //当 删除迭代器所指向的元素的时候,erase删除函数会让it自动下移动 } else { it ++; } } printV(v1); return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run 1 2 3 2 5 6 7 8 9 10 1 3 5 6 7 8 9 10 chunli@http://990487026.blog.51cto.com~/c++$
vector 在首部,尾部插入元素:
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> using namespace std; #include <vector> void printV(vector<int> &v) { for (int i=0; i<v.size(); i++) { cout << v[i] << " "; } cout << endl; } int main() { vector<int> v1(10); v1.insert(v1.begin(), 100); v1.insert(v1.end(), 200); printV(v1); return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run 100 0 0 0 0 0 0 0 0 0 0 200 chunli@http://990487026.blog.51cto.com~/c++$
deque 双端数组容器:尾插,头插,遍历,首部弹出,尾部弹出
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> using namespace std; #include <deque> void printD(deque<int> &d) { for (deque<int>::iterator it=d.begin(); it!=d.end(); it++ ) { cout << *it << " "; } cout << endl; } int main() { deque<int> d1; d1.push_back(1); d1.push_back(3); d1.push_back(5); d1.push_front(-11); d1.push_front(-33); d1.push_front(-55); printD(d1); cout << "头部元素:" << d1.front() <<endl; cout << "尾部元素:" << d1.back() << endl; d1.pop_front(); d1.pop_back(); printD(d1); cout <<endl; return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run -55 -33 -11 1 3 5 头部元素:-55 尾部元素:5 -33 -11 1 3 chunli@http://990487026.blog.51cto.com~/c++$
deque 双端数组容器:查找元素所在容器的位置
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> using namespace std; #include <deque> #include <algorithm> void printD(deque<int> &d) { for (deque<int>::iterator it=d.begin(); it!=d.end(); it++ ) { cout << *it << " "; } cout << endl; } int main() { deque<int> d1; d1.push_back(1); d1.push_back(3); d1.push_back(5); d1.push_front(-11); d1.push_front(-33); d1.push_front(-55); printD(d1); //查找 -33 在数组下标的值 deque<int>::iterator it = find(d1.begin(), d1.end(), -33 ); if (it != d1.end()) { cout << "-33数组下标是" << distance(d1.begin(), it) <<endl; } else { cout << "没有找到值为-33的元素" << endl; } return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run -55 -33 -11 1 3 5 -33数组下标是1 chunli@http://990487026.blog.51cto.com~/c++$
stack容器:入栈,出栈,栈顶,栈大小
chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run 栈的大小是 10 81 64 49 36 25 16 9 4 1 0 栈的大小是 0 chunli@http://990487026.blog.51cto.com~/c++$ chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> using namespace std; #include <stack> int main() { stack<int> s; //入栈 for (int i=0; i<10; i++) { s.push(i * i); //把元素压入 stack容器 } cout << "栈的大小是 " << s.size() << endl; //出栈 while (!s.empty()) { int tmp = s.top(); //获取栈顶元素 cout << tmp << " "; s.pop(); //弹出栈顶元素 } cout << endl; cout << "栈的大小是 " << s.size() << endl; return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run 栈的大小是 10 81 64 49 36 25 16 9 4 1 0 栈的大小是 0 chunli@http://990487026.blog.51cto.com~/c++$
stack装入复杂数据
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> using namespace std; #include <stack> class Teacher { public: int age; char name[32]; public: void printT() { cout << "age:" << age << endl; } }; int main() { Teacher t1, t2, t3; t1.age = 31; t2.age = 32; t3.age = 33; stack<Teacher> s; s.push(t1); s.push(t2); s.push(t3); while (!s.empty()) { Teacher tmp = s.top(); tmp.printT(); s.pop(); } return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run age:33 age:32 age:31 chunli@http://990487026.blog.51cto.com~/c++$
stack装入指针
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> using namespace std; #include <stack> class Teacher { public: int age; char name[32]; public: void printT() { cout << "age:" << age << endl; } }; int main() { Teacher t1, t2, t3; t1.age = 31; t2.age = 32; t3.age = 33; stack<Teacher *> s; s.push(&t1); s.push(&t2); s.push(&t3); while ( !s.empty()) { Teacher *p = s.top(); p->printT(); s.pop(); } return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run age:33 age:32 age:31 chunli@http://990487026.blog.51cto.com~/c++$
queue队列:基本元素 push() front() size() pop()
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> using namespace std; #include <queue> int main() { queue<int> q; q.push(1); q.push(2); q.push(3); cout << "队头元素:" << q.front() << endl; cout << "队列的大小" << q.size() <<endl; while ( !q.empty()) { int tmp = q.front(); cout << tmp << " "; q.pop(); } cout << endl; return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run 队头元素:1 队列的大小3 1 2 3 chunli@http://990487026.blog.51cto.com~/c++$
queue队列:复杂类型
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> using namespace std; #include <queue> class Teacher { public: int age; char name[32]; public: void printT() { cout << "age:" << age << endl; } }; int main() { Teacher t1, t2, t3; t1.age = 31; t2.age = 32; t3.age = 33; queue<Teacher> q; q.push(t1); q.push(t2); q.push(t3); while (!q.empty()) { Teacher tmp = q.front(); tmp.printT(); q.pop(); } return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run age:31 age:32 age:33 chunli@http://990487026.blog.51cto.com~/c++$
queue队列:指针类型
chunli@http://990487026.blog.51cto.com~/c++$ chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> using namespace std; #include <queue> class Teacher { public: int age; char name[32]; public: void printT() { cout << "age:" << age << endl; } }; int main() { Teacher t1, t2, t3; t1.age = 31; t2.age = 32; t3.age = 33; queue<Teacher *> q; q.push(&t1); q.push(&t2); q.push(&t3); while (!q.empty()) { Teacher *tmp = q.front(); tmp->printT(); q.pop(); } return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run age:31 age:32 age:33 chunli@http://990487026.blog.51cto.com~/c++$
list的基本操作,元素的插入,遍历,不支持随机访问
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> using namespace std; #include <list> int main() { list<int> l; cout << "list的大小:" << l.size() << endl; for (int i=0; i<10; i++) { l.push_back(i); //尾部插入元素 尾插法 } cout << "list的大小:" << l.size() << endl; list<int>::iterator it = l.begin(); while (it != l.end()) { cout << *it << " "; it ++; } cout << endl; //list不能随机访问 //0 1 2 3 4 5 // ▲ it = l.begin(); it ++; it ++ ; it ++ ; //it = it + 5; //不支持随机的访问容器 l.insert(it, 100); //请问100插入什么位置 for (list<int>::iterator it=l.begin(); it!=l.end(); it++) { cout << *it <<" "; } cout << endl; //结论1 链表的结点index 序号是从0号位置开始 // 在3号位置插入元素, 让原来的3号位置变成4号位置 原来的4号位置变成5号位置 return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run list的大小:0 list的大小:10 0 1 2 3 4 5 6 7 8 9 0 1 2 100 3 4 5 6 7 8 9 chunli@http://990487026.blog.51cto.com~/c++$
list的删除,移除,
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> using namespace std; #include <list> int main() { list<int> l; cout << "list的大小:" << l.size() << endl; for (int i=0; i<10; i++) { l.push_back(i); //尾部插入元素 尾插法 } cout << "list的大小:" << l.size() << endl; for (list<int>::iterator it=l.begin(); it!=l.end(); it++) { cout << *it <<" "; } cout << endl; //0 1 2 3 4 5 // ▲ list<int>::iterator it1 = l.begin(); list<int>::iterator it2 = l.begin(); it2 ++ ; it2 ++ ; it2 ++ ; l.erase(it1, it2); for (list<int>::iterator it=l.begin(); it!=l.end(); it++) { cout << *it <<" "; } cout << endl; l.insert(l.begin(), 100); l.insert(l.begin(), 100); l.insert(l.begin(), 100); l.erase(l.begin()); // l.remove(100); //2 for (list<int>::iterator it=l.begin(); it!=l.end(); it++) { cout << *it <<" "; } cout << endl; return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run list的大小:0 list的大小:10 0 1 2 3 4 5 6 7 8 9 3 4 5 6 7 8 9 3 4 5 6 7 8 9 chunli@http://990487026.blog.51cto.com~/c++$
优先级队列 priority_queue
chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run 队头元素:55 队列的大小:4 55 33 22 11 测试 最小值优先级队列 最小值优先级队列 队头元素:11 最小值优先级队列 队列的大小:4 11 22 33 55 chunli@http://990487026.blog.51cto.com~/c++$ chunli@http://990487026.blog.51cto.com~/c++$ chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> using namespace std; #include <queue> int main() { priority_queue<int> p1 ; //默认情况下 是 最大值优先级队列 priority_queue<int , vector<int>, less<int> > p2; //提前定义好的预定义函数 谓词 priority_queue<int , vector<int>, greater<int> > p3; //最小值优先级队列 p1.push(33); p1.push(11); p1.push(55); p1.push(22); cout << "队头元素:" << p1.top() <<endl; cout << "队列的大小:" << p1.size() << endl; while (p1.size() > 0 ) { cout << p1.top() << " "; p1.pop(); } cout << "测试 最小值优先级队列 " << endl; p3.push(33); p3.push(11); p3.push(55); p3.push(22); cout << "最小值优先级队列 队头元素:" << p3.top() <<endl; cout << "最小值优先级队列 队列的大小:" << p3.size() << endl; while (p3.size() > 0 ) { cout << p3.top() << " "; p3.pop(); } cout << endl; return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run 队头元素:55 队列的大小:4 55 33 22 11 测试 最小值优先级队列 最小值优先级队列 队头元素:11 最小值优先级队列 队列的大小:4 11 22 33 55 chunli@http://990487026.blog.51cto.com~/c++$
set的基本操作insert,迭代器,erase
1 集合 有序(默认从小到大) 唯一 (红黑二叉树 这种数据结构的变体)
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> #include <stdlib.h> using namespace std; #include <set> int main() { set<int> set1; for (int i=0; i<5; i++) { int tmp = rand(); set1.insert(tmp); } set1.insert(100); set1.insert(100); set1.insert(100); //打印输出 for (set<int>::iterator it = set1.begin(); it != set1.end(); it++) { cout<< *it << " "; } cout << endl; //删除集合 cout << "删除集合\n"; while (!set1.empty()) { set<int>::iterator it = set1.begin(); cout << *it << " "; set1.erase(set1.begin()); } cout << endl; return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run 100 846930886 1681692777 1714636915 1804289383 1957747793 删除集合 100 846930886 1681692777 1714636915 1804289383 1957747793 chunli@http://990487026.blog.51cto.com~/c++$
set的基本操作2:插入,遍历,删除
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> #include <stdlib.h> #include <stdio.h> #include <string.h> using namespace std; #include <set> int main() { set<int> set1; for (int i=0; i<5; i++) { int tmp = rand(); set1.insert(tmp); } //插入元素 重复的 set1.insert(100); set1.insert(100); set1.insert(100); for (set<int>::iterator it=set1.begin(); it!=set1.end(); it++ ) { cout << *it << " "; } cout << endl; //删除集合 while ( !set1.empty()) { set<int>::iterator it = set1.begin(); cout << *it << " "; set1.erase(set1.begin()); } cout << endl; return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run 100 846930886 1681692777 1714636915 1804289383 1957747793 100 846930886 1681692777 1714636915 1804289383 1957747793 chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run
set对基本数据类型,能自动排序:
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> #include <stdlib.h> using namespace std; #include <set> int main() { set<int> set1; set<int, less<int> > set2; //默认情况下是这样 set<int, greater<int> > set3; //从大 到 小 for (int i=0; i<5; i++) { int tmp = rand(); set3.insert(tmp); } //从大 到 小 for (set<int, greater<int> >::iterator it = set3.begin(); it != set3.end(); it++ ) { cout << *it << endl; } return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run 1957747793 1804289383 1714636915 1681692777 846930886 chunli@http://990487026.blog.51cto.com~/c++$
set 2 集合 从大到小
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> #include <stdlib.h> #include <stdio.h> using namespace std; #include <set> int main() { set<int, greater<int> > set1; for (int i=0; i<5; i++) { int tmp = rand(); set1.insert(tmp); } set1.insert(100); set1.insert(100); set1.insert(100); //打印输出 for (set<int, greater<int> >::iterator it = set1.begin(); it != set1.end(); it++) { cout<< *it << " "; } cout << endl; //删除集合 cout << "删除集合\n"; while (!set1.empty()) { set<int, greater<int> >::iterator it = set1.begin(); printf("%d ", *it); set1.erase(set1.begin()); } cout << endl; return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run 1957747793 1804289383 1714636915 1681692777 846930886 100 删除集合 1957747793 1804289383 1714636915 1681692777 846930886 100 chunli@http://990487026.blog.51cto.com~/c++$
set 复杂数据类型
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> #include <stdlib.h> #include <stdio.h> #include <string.h> using namespace std; #include <set> //3 自定义数据类型 排序 //03 仿函数 函数对象 重载() 操作 进行比较大小 //题目:学生包含学号,姓名属性,现要求任意插入几个学生对象到set容器中, //使得容器中的学生按学号的升序排序 class Student { public: Student(const char *name, int age) { strcpy(this->name, name); this->age = age; } protected: public: char name[64]; int age ; }; //函数对象 struct StuFunctor { bool operator()(const Student &left, const Student &right) { return (left.age < right.age); } }; int main() { set<Student, StuFunctor> set1; Student s1("张1", 32); set1.insert(s1); set1.insert(Student("张2", 32) ); set1.insert(Student("张3", 53) ); set1.insert(Student("张4", 34) ); //打印输出 for (set<Student, StuFunctor >::iterator it = set1.begin(); it != set1.end(); it++) { cout<< (*it).name << " "; } cout << endl; return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run 张1 张4 张3 chunli@http://990487026.blog.51cto.com~/c++$
自定义数据类型,仿函数排序法
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> #include <stdlib.h> #include <string.h> using namespace std; #include <set> class Student { public: Student(const char *name, int age) { strcpy(this->name, name); this->age = age; } public: char name[64]; int age; }; //仿函数 struct FuncStudent { bool operator()(const Student &left, const Student &right) { if (left.age < right.age) //如果左边的小 就返回真 从小到大按照年龄进行排序 { return true; } else { return false; } } }; int main() { Student s1("s1", 31); Student s2("s2", 22); Student s3("s3", 44); Student s4("s4", 11); Student s5("s5", 31); set<Student, FuncStudent> set1; set1.insert(s1); set1.insert(s2); set1.insert(s3); set1.insert(s4); set1.insert(s5); //如果两个31岁 能插入成功 //如何知道 插入 的结果 //遍历 for (set<Student, FuncStudent>::iterator it=set1.begin(); it!=set1.end(); it++ ) { cout << it->age << "\t" << it->name << endl; } return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run 11 s4 22 s2 31 s1 44 s3 chunli@http://990487026.blog.51cto.com~/c++$
如何判断 set1.insert函数的返回值
//typedef pair<iterator, bool> _Pairib; //4 如何判断 set1.insert函数的返回值 //Pair的用法 chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> #include <stdlib.h> #include <string.h> using namespace std; #include <set> class Student { public: Student(const char *name, int age) { strcpy(this->name, name); this->age = age; } public: char name[64]; int age; }; //仿函数 struct FuncStudent { bool operator()(const Student &left, const Student &right) { if (left.age < right.age) //如果左边的小 就返回真 从小到大按照年龄进行排序 { return true; } else { return false; } } }; int main() { Student s1("s1", 31); Student s2("s2", 22); Student s3("s3", 44); Student s4("s4", 11); Student s5("s5", 31); set<Student, FuncStudent> set1; pair<set<Student, FuncStudent>::iterator, bool> pair1 = set1.insert(s1); if (pair1.second == true) { cout << "插入s1成功" << endl; } else { cout << "插入s1失败" << endl; } set1.insert(s2); //如何知道 插入 的结果 pair<set<Student, FuncStudent>::iterator, bool> pair5 = set1.insert(s5); //如果两个31岁 能插入成功 if (pair5.second == true) { cout << "插入s1成功" << endl; } else { cout << "插入s1失败" << endl; } //遍历 for (set<Student, FuncStudent>::iterator it=set1.begin(); it!=set1.end(); it++ ) { cout << it->age << "\t" << it->name << endl; } return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run 插入s1成功 插入s1失败 22 s2 31 s1 chunli@http://990487026.blog.51cto.com~/c++$
find查找 equal_range 返回结果是一个pair
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> using namespace std; #include <set> int main() { set<int> set1; for (int i=0; i<10; i++) { set1.insert(i+1); } //从大 到 小 for (set<int>::iterator it = set1.begin(); it != set1.end(); it++ ) { cout << *it << " "; } cout << endl; set<int>::iterator it0 = set1.find(5); cout << "it0:" << *it0 << endl; int num1 = set1.count(5); cout << "num1:" << num1 << endl; set<int>::iterator it1 = set1.lower_bound(5); // 大于等于5的元素 的 迭代器的位置 cout << "it1:" << *it1 << endl; set<int>::iterator it2 = set1.lower_bound(5); // 大于5的元素 的 迭代器的位置 cout << "it2:" << *it2 << endl; //typedef pair<iterator, bool> _Pairib; //typedef pair<iterator, iterator> _Pairii; //typedef pair<const_iterator, const_iterator> _Paircc; //把5元素删除掉 set1.erase(5); pair<set<int>::iterator, set<int>::iterator> mypair = set1.equal_range(5); set<int>::iterator it3 = mypair.first; cout << "it3:" << *it3 << endl; //5 //6 set<int>::iterator it4 = mypair.second; cout << "it4:" << *it4 << endl; //6 //6 return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run 1 2 3 4 5 6 7 8 9 10 it0:5 num1:1 it1:5 it2:5 it3:6 it4:6 chunli@http://990487026.blog.51cto.com~/c++$
set集合的查找:
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> #include <stdlib.h> #include <stdio.h> #include <string.h> using namespace std; #include <set> //3 自定义数据类型 排序 //03 仿函数 函数对象 重载() 操作 进行比较大小 //题目:学生包含学号,姓名属性,现要求任意插入几个学生对象到set容器中, //使得容器中的学生按学号的升序排序 class Student { public: Student(const char *name, int age) { strcpy(this->name, name); this->age = age; } protected: public: char name[64]; int age ; }; //函数对象 struct StuFunctor { bool operator()(const Student &left, const Student &right) { return (left.age < right.age); } }; int main() { int i = 0; set<int> set1; for (i=1; i<10; i++) { set1.insert(i); } //打印输出 for (set<int>::iterator it = set1.begin(); it != set1.end(); it++) { cout<< *it << " "; } cout << endl; set<int>::iterator it1 = set1.lower_bound(5); //大于等于5迭代器 set<int>::iterator it2 = set1.upper_bound(5); //大于5的迭代器 //通过迭代器进行元素的操作 cout<<"it1 "<<*it1<<" "<<"it2 "<<*it2<<endl; pair <set<int>::iterator, set<int>::iterator> pairIt = set1.equal_range(5); set<int>::iterator it3 = pairIt.first; //获取第一个 set<int>::iterator it4 = pairIt.second; //获取第二个 cout<<"it3 "<<*it3<<" "<<"it4 "<<*it4<<endl; return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run 1 2 3 4 5 6 7 8 9 it1 5 it2 6 it3 5 it4 6 chunli@http://990487026.blog.51cto.com~/c++$
multiset基本操练:
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream> #include <stdio.h> using namespace std; #include <set> int main() { multiset<int> set1; int tmp = 0; printf("请输入multiset集合的值:"); scanf("%d", &tmp); while (tmp != 0) { set1.insert(tmp); printf("请输入multiset集合的值:"); scanf("%d", &tmp); } //遍历 for (multiset<int>::iterator it=set1.begin(); it!=set1.end(); it++ ) { cout << *it << " "; } cout << endl; while (!set1.empty()) { multiset<int>::iterator it = set1.begin(); cout << *it << " "; set1.erase(it); } return 0; } chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run 请输入multiset集合的值:4 请输入multiset集合的值:5 请输入multiset集合的值:7 请输入multiset集合的值:83 请输入multiset集合的值:54 请输入multiset集合的值:6 请输入multiset集合的值:4 请输入multiset集合的值:4 请输入multiset集合的值:4 请输入multiset集合的值:0 4 4 4 4 5 6 7 54 83 4 4 4 4 5 6 7 54 83 chunli@http://990487026.blog.51cto.com~/c++$ =====================================