C++ Primer 读书笔记 - 第三章
添加时间:2013-5-20 点击量:
1. std::string
size()函数返回值为string::size_type,用下标时,也用string::size_type作为index的类型
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
int main()
{
string s = abc;
cout << s << endl;
cin >> s;
cout << s << endl;
string line;
while (getline(cin, line)) {
if (line.empty())
break;
cout << line << endl;
}
string::size_type len = s.size();
cout << len << endl;
s[0] = toupper(s[0]);
cout << s << endl;
string s1;
cout << s1[0] << endl;
/ string str = hello + world; / //error: two string literals addition is invalid
return 0;
}
2. std::vector
用push_back()函数插入元素,用下标不克不及用来插入元素,然则map可以,如map[key] = value就相当于把元素插入了。
// an iterator that cannot write elements
vector<int>::const_iterator
// an iterator whose value cannot change
const vector<int>::iterator //不克不及做it++
再就是iterators在vector插入了新的元素后就会失效,然则std::list不会。
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> vec(10, 9);
vector<int>::iterator it = vec.begin();
it++;
cout << it << endl;
vec.push_back(11);
cout << it << endl;
return 0;
}
#include <list>
#include <iostream>
using namespace std;
int main()
{
list<int> mylist;
mylist.push_back(1);
mylist.push_back(2);
mylist.push_back(3);
mylist.push_back(4);
list<int>::iterator it = mylist.begin();
it++;
cout << it << endl;
mylist.push_back(5);
cout << it << endl;
return 0;
}
3. std::bitset
一种是用unsigned value进行初始化,一种是用字符串初始化,比如“0011”,那么bit[0] = str[3]
容易发怒的意思就是: 别人做了蠢事, 然后我们代替他们, 表现出笨蛋的样子。—— 蔡康永
1. std::string
size()函数返回值为string::size_type,用下标时,也用string::size_type作为index的类型
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
int main()
{
string s = abc;
cout << s << endl;
cin >> s;
cout << s << endl;
string line;
while (getline(cin, line)) {
if (line.empty())
break;
cout << line << endl;
}
string::size_type len = s.size();
cout << len << endl;
s[0] = toupper(s[0]);
cout << s << endl;
string s1;
cout << s1[0] << endl;
/ string str = hello + world; / //error: two string literals addition is invalid
return 0;
}
2. std::vector
用push_back()函数插入元素,用下标不克不及用来插入元素,然则map可以,如map[key] = value就相当于把元素插入了。
// an iterator that cannot write elements
vector<int>::const_iterator
// an iterator whose value cannot change
const vector<int>::iterator //不克不及做it++
再就是iterators在vector插入了新的元素后就会失效,然则std::list不会。
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> vec(10, 9);
vector<int>::iterator it = vec.begin();
it++;
cout << it << endl;
vec.push_back(11);
cout << it << endl;
return 0;
}
#include <list>
#include <iostream>
using namespace std;
int main()
{
list<int> mylist;
mylist.push_back(1);
mylist.push_back(2);
mylist.push_back(3);
mylist.push_back(4);
list<int>::iterator it = mylist.begin();
it++;
cout << it << endl;
mylist.push_back(5);
cout << it << endl;
return 0;
}
3. std::bitset
一种是用unsigned value进行初始化,一种是用字符串初始化,比如“0011”,那么bit[0] = str[3]
容易发怒的意思就是: 别人做了蠢事, 然后我们代替他们, 表现出笨蛋的样子。—— 蔡康永