C++ Primer 学习笔记 第九章 容器库概览
9.11 创建vector的六种方式
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> ivc1;
vector<int> ivc2 = {1, 2, 3, 4, 5, 6};
vector<int> ivc3(ivc2);
vector<int> ivc4(ivc2.begin(), ivc2.end()-1); // 指针创建
vector<int> ivc5(10); //申请一个大小10的vector
vector<int> ivc6(10, 3); //申请一个大小10的vector并全部为3
return 0;
}
9.13
#include <iostream>
#include <vector>
#include <list>
using namespace std;
int main() {
list<int> l = {1, 2, 3, 4, 5};
vector<double> vec(l.cbegin(), l.cend());
for(auto x=vec.begin(); x!=vec.end();x++){
cout << *x << " ";
}
cout << endl;
return 0;
}
P302使用swap交换两个vector里面的值
#include <iostream>
#include <vector>
#include <list>
using namespace std;
int main() {
vector<int> vec(2, 1);
vector<int> vec2(5, 4);
swap(vec, vec2);
for (auto iter=vec2.begin(); iter!=vec2.end(); iter++){
cout << *iter << " ";
}
cout << endl;
return 0;
}
P303 assign可以接收不同类型的容器,做隐式转换后赋值
#include <iostream>
#include <vector>
#include <list>
using namespace std;
int main() {
list<string> names;
vector<const char*> oldstyle;
// names = oldstyle;
names.assign(oldstyle.cbegin(),oldstyle.cend());
return 0;
}
9.15 两个容器是否相等
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> vec1 = {1, 2};
vector<int> vec2 = {1, 2};
if (vec1 == vec2) {
cout << "vec1 == vec2" << endl;
}
return 0;
}
9.16
#include <iostream>
#include <vector>
#include <list>
using namespace std;
int main() {
vector<int> vec1 = {1, 2};
list<int> list1 = {1, 2};
vector<int> vec2(list1.begin(), list1.end());
if (vec1 == vec2){
cout << "list1 == vec1" << endl;
}
return 0;
}
容器操作api
308 push_x insert
#include <iostream>
#include <vector>
#include <list>
#include <deque>
using namespace std;
int main() {
deque<int> que = {1,};
// push_back
que.push_back(2);
// push_front, only support by deque, not vector
que.push_front(0);
// insert : insert element at the font of container
que.insert(que.cbegin(), -1);
// anther usage of inter op, setting count num and value
que.insert(que.end(), 2, 3);
// customize insert
deque<int> que2 = {0, 0, 0, 4, 5, 6};
que.insert(que.end(), que2.begin()+3, que2.end());
// error, insert position and begin position should not be same iterator
que.insert(que.begin(), que.begin(), que.end());
for (deque<int>::iterator iter1 = que.begin(); iter1 != que.end(); iter1++){
cout << *iter1 << " ";
}
cout << endl;
return 0;
}
#include <iostream>
#include <vector>
#include <list>
using namespace std;
int main() {
list<char> lst;
list<char>::iterator iter = lst.begin();
char word;
while(cin >> word){
iter = lst.insert(iter, word);
for (auto value:lst){
cout << value << " ";
}
cout << endl;
}
return 0;
}
9.18
#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <deque>
using namespace std;
using namespace std;
int main() {
string s;
deque<string> ans;
while (cin >> s) {
ans.push_back(s);
}
for (auto it = ans.begin();it != ans.end();++it) {
cout << *it << endl;
}
system("pause");
return 0;
}
9.20
#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <deque>
using namespace std;
using namespace std;
int main() {
list<int> lst = {1, 2, 3, 4, 5, 6, 7, 8};
deque<int> q1;
deque<int> q2;
for (list<int>::iterator iter=lst.begin(); iter != lst.end(); ++iter){
if (*iter % 2 == 0){
q2.push_back(*iter);
} else{
q1.push_back(*iter);
}
}
cout << "q1" << " ";
for (auto val : q1){
cout << val << " ";
}
cout << endl << "q2" << " ";
for (auto val : q2){
cout << val << " ";
}
cout << endl;
return 0;
}
309 front back
#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <deque>
using namespace std;
using namespace std;
int main() {
vector<int> vec = {1, 2, 3, 4, 5, 6, 7, 8};
// front
cout << *vec.begin() << " " << vec.front() << endl;
// back
cout << *(--vec.end()) << " " << vec.back() << endl;
// return reference from back and front api
auto &v = vec.front();
v = 0;
cout << *vec.begin() << endl;
return 0;
}
9.24
#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <deque>
using namespace std;
using namespace std;
int main() {
vector<int> vec = {1, 2, 3, 4, 5, 6, 7, 8};
vector<int> vec2;
cout << vec.at(0) << " " << vec.front() << " " << *vec.begin();
// cout << *vec2.begin() << endl;
return 0;
}
312 删除所有的奇数
#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <deque>
using namespace std;
using namespace std;
int main() {
vector<int> vec = {1, 2, 3, 4, 5, 6, 7, 8};
auto it = vec.begin();
while (it != vec.end()){
if (*it % 2){
// del it and iterator moves to the next element
it = vec.erase(it);
} else{
++it;
}
}
for (int val:vec){
cout << val << " ";
}
cout << endl;
// del all elements
vec.clear();
return 0;
}
9.26
#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <deque>
using namespace std;
using namespace std;
int main() {
int ia[] = { 0,1,1,2,3,5,8,13,21,55,89 };
vector<int> vec(ia, end(ia));
for (auto iter1=vec.begin(); iter1 !=vec.end(); ){
if (*iter1 % 2 == 0){
iter1 = vec.erase(iter1);
} else{
++ iter1;
}
}
for (int val:vec){
cout << val << " ";
}
cout << endl;
list<int> lst(begin(ia), end(ia));
for (list<int>::iterator iter2=lst.begin(); iter2!=lst.end();){
if (*iter2%2){
iter2 = lst.erase(iter2);
}else{
++iter2;
}
}
for (int val:lst){
cout << val << " ";
}
cout << endl;
return 0;
}
P313 特殊的forward_list
#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <deque>
#include <forward_list>
using namespace std;
using namespace std;
int main() {
forward_list<int> flst = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
// off the beginning
auto prev = flst.before_begin();
auto curr = flst.begin();
while (curr!=flst.end()){
if (*curr%2){
curr = flst.erase_after(prev);
}else{
prev = curr;
++curr;
}
cout << "*prev: " << *prev << " " << "*curr: " << *curr << endl;
}
for (int val:flst){
cout << val << " ";
}
cout << endl;
return 0;
}
9.27
#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <deque>
#include <forward_list>
using namespace std;
using namespace std;
int main() {
forward_list<int> flst = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
auto prev = flst.before_begin();
auto curr = flst.begin();
while(curr!=flst.end()){
if (*curr%2){
curr = flst.erase_after(prev);
} else{
++prev;
++curr;
}
}
for (int val:flst){
cout << val << " ";
}
cout << endl;
return 0;
}
9.28
#include <iostream>
#include <vector>
#include <string>
#include <forward_list>
using namespace std;
forward_list<string> flst_insert(forward_list<string> flst, string s1, string s2){
auto prev = flst.before_begin();
auto curr = flst.begin();
bool flag = false;
while (curr!=flst.end()){
if (*curr == s1) {
curr = flst.insert_after(curr, s2);
flag = true;
break;
} else{
++ curr;
++ prev;
}
}
if (flag == false){
flst.insert_after(prev, s2);
}
return flst;
}
int main() {
forward_list<string> f = {"a", "b", "c", "d"};
f = flst_insert(f, "s", "x");
for (string s:f){
cout << s << " ";
}
cout << endl;
return 0;
}
P314
#include <iostream>
#include <vector>
#include <string>
#include <forward_list>
#include <list>
using namespace std;
int main() {
list<int> ilist(10, 42);
ilist.resize(15);
for (int val:ilist){
cout << val << " ";
}
cout << endl;
// 42 42 42 42 42 42 42 42 42 42 0 0 0 0 0
ilist.resize(25, -1);
for (int val:ilist){
cout << val << " ";
}
cout << endl;
// 42 42 42 42 42 42 42 42 42 42 0 0 0 0 0 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
ilist.resize(5);
for (int val:ilist){
cout << val << " ";
}
cout << endl;
// 42 42 42 42 42
return 0;
}
9.31
#include <iostream>
#include <vector>
#include <string>
#include <forward_list>
#include <list>
using namespace std;
int main() {
list<int> vi ={0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
auto iter = vi.begin();
while (iter != vi.end()){
if (*iter%2) {
iter = vi.insert(iter, *iter);
++iter;
++iter;
}else{
iter = vi.erase(iter);
}
}
for (auto val:vi){
cout << val << " ";
}
cout << endl;
return 0;
}
#include <iostream>
#include <vector>
#include <string>
#include <forward_list>
#include <list>
using namespace std;
int main() {
forward_list<int> vi ={0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
auto iter = vi.begin();
auto prev = vi.before_begin();
while (iter != vi.end()){
if (*iter%2) {
iter = vi.insert_after(iter, *iter);
prev = iter;
iter ++;
}else{
iter = vi.erase_after(prev);
}
}
for (auto val:vi){
cout << val << " ";
}
cout << endl;
return 0;
}
P319容器的内存分配
#include <iostream>
#include <vector>
#include <string>
#include <forward_list>
#include <list>
using namespace std;
int main() {
vector<int> ivec;
cout << " ivec: size: " << ivec.size() << " capacity: " << ivec.capacity() << endl;
// ivec: size: 0 capacity: 0
for(vector<int>::size_type ix=0; ix!=24; ++ix){
ivec.push_back(ix);
}
cout << " ivec: size: " << ivec.size() << " capacity: " << ivec.capacity() << endl;
// ivec: size: 24 capacity: 28
ivec.reserve(50);
cout << " ivec: size: " << ivec.size() << " capacity: " << ivec.capacity() << endl;
// ivec: size: 24 capacity: 50
while (ivec.size()!=ivec.capacity()){
ivec.push_back(0);
}
cout << " ivec: size: " << ivec.size() << " capacity: " << ivec.capacity() << endl;
// ivec: size: 50 capacity: 50
ivec.push_back(42);
cout << " ivec: size: " << ivec.size() << " capacity: " << ivec.capacity() << endl;
// ivec: size: 51 capacity: 75
ivec.shrink_to_fit();
cout << " ivec: size: " << ivec.size() << " capacity: " << ivec.capacity() << endl;
// ivec: size: 51 capacity: 51
return 0;
}
9.39
#include <iostream>
#include <vector>
#include <string>
#include <forward_list>
#include <list>
using namespace std;
int main() {
vector<string> v;
for (string buffer; cin >> buffer; v.push_back(buffer)){
cout << v.size() << " " << v.capacity() << endl;
}
return 0;
}
P321 char* char[]和string相关转换
#include <iostream>
#include <vector>
#include <string>
#include <forward_list>
#include <list>
using namespace std;
int main() {
const char *cp = "Hello World!!!";
string s1(cp);
cout << s1 << endl;
//Hello World!!!
string s3(cp+6, 5);
cout << s3 << endl;
// World
string s4(s1, 6 ,5);
cout << s4 << endl;
// World
string s5 = s1.substr(6, 5);
cout << s5 << endl;
// World
return 0;
}
9.41
#include <iostream>
#include <vector>
#include <string>
#include <forward_list>
#include <list>
using namespace std;
int main() {
vector<char> vec = {'a', 'b', 'c'};
string s(vec.begin(), vec.end());
cout << s << endl;
return 0;
}
P322 字符串string的相关操作
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main() {
const char *cp = "Stately, plump Buck";
string s;
s.assign(cp, 7);
cout << s << endl; // Stately
s.insert(s.size(), cp+7);
cout << s << endl; // Stately, plump Buck
string s1 = "some string", s2 = "some other string";
s1.insert(0, s2);
cout << s1 << endl; // some other stringsome string
s1 = "some string";
s1.insert(0, s2, 0, s2.size());
cout << s1 << endl; // some other stringsome string
return 0;
}
append, replace, erase
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main() {
// append
string s("C++ Primer"), s2 = s;
s.insert(s.size(), " 4th Ed.");
cout << s << endl; // C++ Primier 4th Ed.
s2.append(" 4th Ed.");
cout << s << endl;
s.erase(11, 3);
s.insert(11, "5th");
cout << s << endl; // C++ Primer 5th Ed.
s2.replace(11, 3, "5th");
cout << s2 << endl; // C++ Primer 5th Ed.
s.replace(11, 3, "Fifth");
cout << s << endl; // C++ Primer Fifth Ed.
return 0;
}
9.43
#include <iostream>
#include <vector>
#include <string>
using namespace std;
void replace_str(string &s, string &oldVal, string &newVal){
auto iter = s.begin();
while (iter+oldVal.size()!=s.end()){
if (oldVal == string(iter, iter+oldVal.size())){
iter = s.erase(iter, iter + oldVal.size());
iter = s.insert(iter, newVal.begin(), newVal.end());
iter += newVal.size();
} else{
++ iter;
}
}
}
int main() {
string s("though,you don't love me");
string oldVal("though");
string newVal("tho");
replace_str(s, oldVal, newVal);
cout << s;
return 0;
}
9.44
#include <iostream>
#include <vector>
#include <string>
using namespace std;
void replace_str(string &s, string &oldVal, string &newVal){
auto iter = s.begin();
while (iter+oldVal.size()!=s.end()){
if (oldVal == string(iter, iter+oldVal.size())){
s.replace(iter, iter+oldVal.size(), newVal);
iter += newVal.size();
} else{
++ iter;
}
}
}
int main() {
string s("though,you don't love me");
string oldVal("though");
string newVal("tho");
replace_str(s, oldVal, newVal);
cout << s;
return 0;
}
9.45
#include <iostream>
#include <vector>
#include <string>
using namespace std;
void add_great(string &name, string &prefix, string &suffix){
name.append(" ");
name.append(suffix);
name.insert(0, " ");
name.insert(0, prefix);
}
int main() {
string s("Jack");
string prefix("Mr");
string suffix("II");
add_great(s, prefix, suffix);
cout << s;
return 0;
}
9.45
#include <iostream>
#include <vector>
#include <string>
using namespace std;
void add_great(string &name, string &prefix, string &suffix){
name.insert(name.size(), " ");
name.insert(name.size(), suffix);
name.insert(0, " ");
name.insert(0, prefix);
}
int main() {
string s("Jack");
string prefix("Mr");
string suffix("II");
add_great(s, prefix, suffix);
cout << s;
return 0;
}
P325 字符串搜索
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
int main() {
// find string
string name("AnnaBelle");
auto pos1 = name.find("Belle");
cout << pos1 << endl; // 4
string lowercase("annabelle");
auto pos2 = lowercase.find("Anna"); // 4294967295
cout << pos2 << endl;
string numbers("0123456789"), s2("r2d2");
// find first number's position in s2
auto pos3 = s2.find_first_of(numbers);
cout << pos3 << endl; // 1
string dept("03714p3");
// find first not number's position in s2
auto pos4 = dept.find_first_not_of(numbers);
cout << pos4 << endl; // 5
return 0;
}
通过循环查找所有的字符串
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
int main() {
// find string
string s1("AnnaBelle");
string ch = "e";
string::size_type pos = 0;
// npos: if can not find any match, return npos
while((pos = s1.find_first_of(ch, pos)) != string::npos){
cout << "found alphabet " << s1[pos] << " at index: " << pos << endl;
//found alphabet e at index: 5
//found alphabet e at index: 8
++ pos;
}
return 0;
}
9.47
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
int main() {
string s1("ab2c3d7R4E6");
string number("0123456789");
string::size_type pos = 0;
while ((pos = s1.find_first_of(number, pos)) != string::npos){
cout << "number: " << s1[pos] << " position: " << pos << endl;
++pos;
}
string alphabet{ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" };
pos = 0;
while ((pos = s1.find_first_not_of(alphabet, pos)) != string::npos){
cout << "number: " << s1[pos] << " position: " << pos << endl;
++pos;
}
return 0;
}
328 string和其他类型之间的转换
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
int main() {
int i = 42;
string s = to_string(i);
cout << s << endl;
double d = stod(s);
cout << d << endl;
string s2 = "pi = 3.14";
d = stod(s2.substr(s2.find_first_of("+-.0123456789")));
cout << d << endl;
string i1 = "5";
cout << stoi(i1) << endl;
return 0;
}
9.50
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
int main() {
vector<string> vec = {"1", "2", "3", "4", "5"};
int sum = 0;
for (string val:vec){
sum += stoi(val);
}
cout << "sum of vector " << sum << "." << endl;
return 0;
}
330 stack和queue
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <stack>
#include <queue>
using namespace std;
int main() {
// 先进后出
stack<int> intStack;
for (size_t ix=0; ix != 10; ++ix){
intStack.push(ix);
}
while (!intStack.empty()){
int value = intStack.top();
cout << value << endl;
intStack.pop();
}
// 先进先出
queue<int> intQueue;
for (int ix=0; ix!=10; ++ix){
intQueue.push(ix);
}
while (!intQueue.empty()){
cout << intQueue.front() << endl;
intQueue.pop();
}
return 0;
}
9.52
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
int main() {
stack<char> StringStack;
string expr = "sfs(1 + 1 = 2)eweqgrt(4 + 1 = 5)";
bool flag = false;
for (int i=0; i!=expr.size();++i){
char s = expr[i];
if (s == '(' || flag == true){
StringStack.push(expr[i]);
if (s == '('){
flag = true;
}
else if(s == ')'){
flag = false;
StringStack.pop();
cout << StringStack.top() << endl;
while (!StringStack.empty()){
StringStack.pop();
}
}
}
}
return 0;
}