C++ 11 泛型算法

插入迭代器

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
#include <iostream>
#include <sstream>
#include <fstream>
#include <set>
#include <vector>
#include <list>
#include <map>
#include <string>
#include <iterator> // std::front_inserter

using namespace std;

int main()
{
// 插入迭代器
list<int> ilist;
vector<int> ivec;
ivec.push_back(1);
ivec.push_back(42);
ivec.push_back(3);
// 利用插入迭代器拷贝 , 将ivec元素依次插入到ilist前面
copy(ivec.begin(), ivec.end(), front_inserter(ilist));
// ilist输出为 3, 42 ,1
for (list<int>::iterator i = ilist.begin(); i != ilist.end(); ++i)
cout << "index i : " << *i << endl;

// 从ivec找到元素42的位置,拷贝42与之后的元素 42,3
ilist.clear();
vector<int>::iterator it = find(ivec.begin(), ivec.end(), 42);
copy(it, ivec.end(), front_inserter(ilist)); // 拷贝指定范围
// ilist输出为42 ,3
for (list<int>::iterator i = ilist.begin(); i != ilist.end(); ++i)
cout << "index i : " << *i << endl;

return 0;
}

流迭代器

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
#include <iostream>
#include <sstream>
#include <fstream>
#include <set>
#include <vector>
#include <list>
#include <map>
#include <string>
#include <iterator> // std::front_inserter
#include "Sales_item.h"

using namespace std;

int main()
{
// 流迭代器定义
cout << "int类型流迭代器,输入数字:" << endl;

istream_iterator<int> cin_it(cin); // 从输入流中读取int值
istream_iterator<int> eof; // 输入流终止符

while (cin_it != eof)
{
int input = *cin_it++;
if (input == 0)break;

cout << "input:" << input << endl; // 会显示输入的上一个?
}
cin.clear();
cout << "对象类型流迭代器,输入字符串 整数 浮点数:" << endl;
istream_iterator<Sales_item> item_iter(cin), item_eof;
Sales_item sum;
sum = *item_iter++;
while (item_iter != item_eof)
{
if (item_iter -> same_isbn(sum))
{
sum = sum + *item_iter;
}
else
{
cout << sum << endl;
sum = *item_iter;
}
++item_iter;
}
cout << sum << endl;
return 0;
}

流迭代器 与算法

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
#include <iostream>
#include <sstream>
#include <fstream>
#include <set>
#include <vector>
#include <list>
#include <map>
#include <string>
#include <iterator> // std::front_inserter
#include <algorithm>

using namespace std;

int main()
{
// 流迭代器 与算法
cout << "int类型流迭代器,输入数字:" << endl;

istream_iterator<int> cin_it(cin); // 从输入流中读取int值
istream_iterator<int> eof; // 输入流终止符

vector<int> vec(cin_it, eof); //将输入数字转入容器
sort(vec.begin(), vec.end());
ostream_iterator<int> output(cout, " "); // 以空格为输出元素分隔符
unique_copy(vec.begin(), vec.end(), output); // 容器去重拷贝到输出流

return 0;
}

反向迭代器

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
#include <iostream>
#include <sstream>
#include <fstream>
#include <set>
#include <vector>
#include <list>
#include <map>
#include <string>
#include <iterator> // std::front_inserter
#include <algorithm>

using namespace std;

int main()
{
cout << "反向迭代器:" << endl;
vector<int> ivec;
ivec.push_back(1);
ivec.push_back(2);
ivec.push_back(3);

ostream_iterator<int> output(cout,",");
copy(ivec.begin(), ivec.end(), output); // 打印数组不错哎

cout << "用反向迭代器逆序输出: " << endl;
for (vector<int>::reverse_iterator it = ivec.rbegin();
it != ivec.rend(); ++it)
{
cout << *it << ",";
}
cout << "用反向迭代器拷贝到输出流:" << endl;
copy(ivec.rbegin(), ivec.rend(), output);

return 0;
}

C++ 10 关联容器

map 使用

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
#include <iostream>
#include <utility>
#include <map>
#include <string>
using std::pair; using std::map;
using std::string; using std::cin; using std::cout; using std::endl;

int main()
{
pair<string, string> author("Jom", "Tom");
author = std::make_pair("Tom", "Jerry"); // pair 对象使用
cout << "author : " << author.first << "&" << author.second << endl;

map<string, int> word_cout;
word_cout["A"] = 1; // map 对象使用
map<string, int>::iterator map_it = word_cout.begin();
cout << map_it->first << ":" << map_it->second << endl;
++word_cout["A"];
cout << "count for A: " << word_cout["A"] << endl;

word_cout.insert(map<string, int>::value_type("B", 1));
pair<map<string, int>::iterator, bool> ret = word_cout.insert(std::make_pair("B", 1)); // map插入 返回插入键迭代器和 是否插入
if (!ret.second) ++ret.first->second; // 若键已存在 , 则+1

int occurs = 0;
if (word_cout.count("B"))
occurs = word_cout["B"]; // map 统计是否存在
map<string, int>::iterator it = word_cout.find("A"); // map查找 若不存在 等于尾迭代器
if (it != word_cout.end())
occurs = it->second;

if (word_cout.erase("A")) // map删除 0,1
cout << "delete success : " << "A" << endl;
else
cout << "delete word not found" << endl;

map<string, int>::const_iterator map_iter = word_cout.begin(); // map 遍历
while (map_iter != word_cout.end()) {
cout << map_iter->first << " : " << map_iter->second << endl;
++map_iter;
}

return 0;
}

单词统计

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include<fstream>
#include <utility>
#include <map>
#include <string>
using std::pair; using std::map;
using std::string; using std::cin; using std::cout; using std::endl;

int main()
{
std::ifstream in_file("out.txt");
map<string, int> word_count;
string word;
while (in_file >> word) {
pair<map<string, int>::iterator, bool> ret = word_count.insert(std::make_pair(word,1));
if (!ret.second) ++ret.first->second;
}
for (map<string, int>::iterator it = word_count.begin(); it != word_count.end(); ++it)
{
cout << it->first << ":" << it->second << endl;
}

return 0;
}

multimap、multiset用法

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
#include <iostream>
#include<fstream>
#include <utility>
#include <set>
#include <map>
#include <string>
using std::pair; using std::map; using std::set; using std::multimap; using std::multiset;
using std::string; using std::cin; using std::cout; using std::endl;

int main()
{
set<string> set1;
set1.insert("A");
set<string> set2;
set2.insert(set1.begin(), set1.end()); // set 插入

set<string>::iterator it = set2.find("A");
set2.count("A"); // 0 , 1

multimap<string, string> mapping;
mapping.insert(std::make_pair("A", "B"));
mapping.insert(std::make_pair("A", "C")); // multimap 插入
string search_item("A");
multimap<string, string>::size_type enties = mapping.count(search_item); // multimap 统计
multimap<string, string>::iterator it2 = mapping.find(search_item); // multimap 查找
for (multimap<string, string>::size_type i = 0; i != enties; ++i, ++it2)
cout << it2->first << ":" << it2->second << endl;

multimap<string, string>::iterator beg = mapping.lower_bound(search_item), end = mapping.upper_bound(search_item); //multimap 读取键对应范围
while (beg != end) {
cout << beg->first << ":" << beg->second << endl;
++beg;
}
typedef multimap<string, string>::iterator map_it;
pair<map_it, map_it> pos = mapping.equal_range(search_item); // multimap一次性获取键对应范围
while (pos.first != pos.second)
{
cout << pos.first->first << ":" << pos.first->second << endl;
++pos.first;
}
multimap<string, string>::size_type cnt = mapping.erase(search_item); // multimap 删除
cout << "remove :" << cnt << endl;

return 0;
}

C++ 09 顺序容器

顺序容器初始化

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
#include <iostream>
#include <vector>
#include <list>
#include <deque>
#include <string>
using std::vector; using std::list; using std::deque;
using std::string; using std::cin; using std::cout; using std::endl;

int main()
{
// 顺序容器初始化,默认构造函数
vector<string> svec;
list<int> ilist;
deque<string> items;
// 容器元素初始化
vector<int> ivec;
vector<int> ivec2(ivec); // 将一个容器初始化为另一个容器副本
list<string> slist(svec.begin(), svec.end()); // 初始化为一段元素的副本
vector<string>::iterator mid = svec.begin() + svec.size() / 2;
deque<string> front(svec.begin(), mid); // 初始化为一半
deque<string> back(mid, svec.end()); // 初始化为后一半

char *words[] = { "red","pink","green" };
size_t words_size = sizeof(words) / sizeof(char *);
list<string> words2(words, words + words_size); // string 数据 转list

const list<int>::size_type list_size = 42; // 分配并初始化指定数目元素
list<string> slist(list_size, "oh"); // 42 string each is oh

list<int> ilist2(list_size); // 指定大小

return 0;
}

容器的增删改查

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <vector>
#include <list>
#include <deque>
#include <string>
using std::vector; using std::list; using std::deque;
using std::string; using std::cin; using std::cout; using std::endl;

int main()
{

vector<int> vec(2, 5);
// 顺序容器迭代器都支持自增自减、相等判断、解引用操作
// vector 和 deque 支持额外操作 加减大小判断
vector<int>::iterator iter = vec.begin() + vec.size() / 2;
list<int> ilist;
ilist.push_back(1);
ilist.push_back(2); // 尾插入
ilist.push_front(3); // 前插入 3,1,2
list<int>::const_iterator it = ilist.begin();
it = ilist.insert(it, 4); // 指定迭代器前插入一个 4,3,1,2
ilist.insert(it, 3, 3); // 指定迭代器前插入n个value 3,3,3,4,3,1,2
ilist.insert(it, vec.begin(), vec.end()); //指定迭代器前插入一段 3,3,3,5,5,4,3,1,2

for (list<int>::const_iterator iter = ilist.begin(); iter != ilist.end(); iter++) // 正序输出list
{
cout << *iter << ",";
}
cout << endl << "逆序:" << endl;
for (list<int>::const_reverse_iterator iter = ilist.rbegin(); iter != ilist.rend(); iter++) // 逆序输出list
{
cout << *iter << ",";
}
cout << endl;
// 容器大小操作
cout << "list 元素个数:" << ilist.size() << endl;
cout << "list maxsize :" << ilist.max_size() << endl;
cout << "list is empty:" << ilist.empty() << endl;

list<int> ilist2(10, 42);
ilist2.resize(15); // add 5 elemet of value 0 back of list
ilist2.resize(25, -1); // add 10 elemets of value -1 back of list
ilist2.resize(5); // erases 20 elements from the back of list

// 访问元素
vector<string> svec;
svec.push_back("a");
svec.push_back("b");
vector<string>::reference val1 = svec.front();
vector<string>::reference val2 = svec.back();
cout << "first element : " << val1 << endl;
cout << "last elememt : " << val2 << endl;
cout << "second element : " << svec[1] << endl;

// 删除元素
deque<int> id(5,2);
while (id.empty())
{
cout << "id's element : " << id.front() << endl;
id.pop_front();
}

list<string> slist(10, "a");
slist.back() = "b";
string serachValue("b");
list<string>::iterator it2 = std::find(slist.begin(), slist.end(), serachValue);
if (it2 != slist.end()) slist.erase(it2); // 找到并删除
for (list<string>::const_iterator iter = slist.begin(); iter != slist.end(); iter++) // 正序输出list
{
cout << *iter << ",";
}
slist.clear(); // clear list

list<string> slist2(2, "a");
list<string> slist3(2, "b");
slist2 = slist3; // 清空slist2 , 将slist3元素赋值给slist2
slist2.swap(slist3); // 交换内容
slist2.assign(slist3.begin(), slist3.end()); // 使用一段内容将slist2重新设置
slist2.assign(2, "c"); // 使用2个c重新设置slist2
return 0;
}

string 对象与容器

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
#include <iostream>
#include <vector>
#include <list>
#include <deque>
#include <string>
using std::vector; using std::list; using std::deque;
using std::string; using std::cin; using std::cout; using std::endl;

int main()
{
// 构造string对象
string s1;
string s2(5, 'a');
string s3(s2);
string s4(s3.begin(), s3.begin() + s3.size() / 2);
// 用紫川初始化
s1 = "nioh";
string s6(s1, 2); // oh
string s7(s1, 0, 2); // ni
string s8(s1, 0, 8); // nioh

s2.erase(s2.size() - 5, 5); // 删除最后5个元素
s2.insert(s2.size(), 5, '!');//最后添加5个!

char *cp = "nihaoa! xiaohui";
string s;
s.assign(cp, 7);// nihaoa!
s.insert(s.size(), cp + 7); // s尾添加 cp[7]之后的元素
cout << "after insert s: " << s << endl;

s = "hello world";
string ss2 = s.substr(6, 5); // world
cout << "ss2: " << ss2 << endl;
cout << "ss2.append('!') = " << ss2.append("!") << endl;
cout << "s.replace(6,5,'xiaohui') = " << s.replace(6, 5, "xiaohui") << endl;

// 查找
string name("Xiaohui");
string::size_type pos1 = name.find("hui"); // 4

string numerics("0123456789");
string name2("r2d2");
string::size_type pos = name2.find_first_of(numerics);
cout << "fount at index : " << pos << " element is " << name2[pos] << endl; // 匹配点1,2

string name3("49p123");
pos = name3.find_first_not_of(numerics);
cout << "find at index : " << pos << "element is " << name3[pos] << endl; // 不匹配点 2,p

string sp1("aab");
string sp2("ab");
cout << "sp1.compare(sp2) = " << sp1.compare(sp2) << endl;
cout << "sp1.compare(1,2,sp2) = " << sp1.compare(1, 2, sp2) << endl;
cout << " sp1.compare(1, 2, sp2, 0, 2) = " << sp1.compare(1, 2, sp2, 0, 2) << endl;
return 0;
}

容器适配器 stack 、 quene

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
#include <iostream>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <string>
using std::vector; using std::list; using std::deque; using std::stack;
using std::string; using std::cin; using std::cout; using std::endl;

int main()
{

deque<int> deq;
stack<int> stk(deq);
stack<string, vector<string> > str_stk;

const stack<int>::size_type stk_size = 10;
stack<int> intStack;
int ix = 0;
while (intStack.size() != stk_size)
intStack.push(ix++);

int error_cnt = 0;
while (intStack.empty() == false) {
int value = intStack.top();
if (value != --ix) {
std::cerr << "oops expected " << ix << " received " << value << endl;
++error_cnt;
}
intStack.pop();
}
cout << "out program ran with " << error_cnt << " errors" << endl;
return 0;
}

C++ 08 标准IO库

特点

  1. IO对象不可复制或赋值
  2. ifstream 由 istream 派生,提供读文件功能。
  3. ofstream 由 ostream 派生,提供写文件功能。
  4. fstream 由 iostream 派生,读写同一个文件。

流状态查询控制

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
#include <iostream>
#include <string>

using std::string;
using std::cin;
using std::cout;
using std::endl;


int main()
{
int ival;
while (cin >> ival, !cin.eof()) // 输入输出绑定 cout关联缓冲区被刷新
{
if (cin.bad()) throw std::runtime_error("IO stream corrupted"); // 流状态检查
if (cin.fail()) {
std::cerr << "bad data , try again";
cin.clear(std::istream::failbit);// 重置输入流
std::istream::iostate old_state = cin.rdstate(); // 流当前整个条件状态
cin.clear(old_state);
continue;
}
cout << "ok," << ival << endl;
cout << "hi" << std::flush; // 刷新 无添加
cout << "hi" << std::ends; // 刷新 添加null
cout << "hi" << std::endl; // 刷新 添加换行
}
return 0;
}

文件流对象使用

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
#include <iostream>
#include <fstream>
#include <string>
using std::ifstream; using std::ofstream; using std::fstream;
using std::string;using std::cin;using std::cout;using std::endl;

int main()
{
string file1 = "/share/PostLoanData/file.txt";
string file2 = "/share/PostLoanData/file2.txt";
ifstream infile(file1.c_str()); // 打开特点文件
ofstream outfile(file2.c_str());

ifstream infile2;
ofstream outfile2;
infile2.open(file1); // 打开流 再捆绑文件
outfile2.open(file2);
// 判断文件是否打开成功
if (!infile) {
std::cerr << "error: unable to open input file:" << file1 << endl;
return -1;
}
infile.close();
infile.open(file2);// 文件流重新捆绑

infile2.close(); // 清空流
return 0;
}

文件模式

模式 释义
in 打开文件读,ifstream流对象默认模式
out 打开文件写,文件会被清空
app 文件尾追加
ate 打开文件后定位文件尾
trunc 打开文件时清空文件流
binary 二进制模式IO操作
out + app 打开文件写操作,文件尾写入
in + out 打开文件做读写操作,定位文件开头
out + app + trunc 打开文件做读写操作,删除已有数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>
#include <string>
using std::ifstream; using std::ofstream; using std::fstream;
using std::string;using std::cin;using std::cout;using std::endl;

int main()
{
string file1 = "/share/PostLoanData/file.txt";
string file2 = "/share/PostLoanData/file2.txt";
ofstream outfile(file1 , ofstream::app); // 以追加模式打开
outfile << "app add in end" << endl;
outfile.close();
ifstream infile(file1);
string line;
while (infile >> line) {
cout << line ;
}
infile.close();
return 0;
}

字符串流

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <sstream>
#include <string>
using std::istringstream; using std::ostringstream;
using std::string; using std::cin; using std::cout; using std::endl;

int main()
{
ostringstream os;
os << "val1: " << 1024 << "\n"
<< "val2: " << 42 << "\n";
istringstream is(os.str());
string dump;
int val1, val2;
is >> dump >> val1 >> dump >> val2;
cout << val1 << "," << val2 << endl;
return 0;
}

C++ 07 函数

定义

函数者,黑盒是也。

参数传递

形参初始化:若形参为非引用类型,复制实参值,若未引用类型,为实参的别名。

指针形参与const形参

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
#include <iostream>

using std::cin;
using std::cout;
using std::endl;

void swap(int * a, int * b); // 指针作为形参
void print(const int * a, const int * b); // const使参数不能被改变

int main()
{
int a = 1, b = 2;
print(&a, &b);
swap(&a, &b);
print(&a, &b);
}

void swap(int * a, int * b)
{
int temp = *a;
*a = *b;
*b = temp;
}

void print(const int * a, const int * b)
{
cout << "a :" << *a << ", b : " << *b << 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
40
41
42
43
#include <iostream>
#include <vector>

using std::cin;
using std::cout;
using std::endl;
using std::vector;

vector<int>::const_iterator find_val(
vector<int>::const_iterator beg,
vector<int>::const_iterator end,
int value,
vector<int>::size_type &occurs);

int main()
{
vector<int> vi(10);
vi[3] = 3;

for (vector<int>::iterator iter = vi.begin(); iter != vi.end(); ++iter)
cout << *iter << ",";
cout << endl;
vector<int>::size_type occurs;
vector<int>::const_iterator citer = find_val(vi.begin(), vi.end(), 3, occurs);
cout << "find them : " << *citer << " , occurs time : " << occurs << endl;
}

// 从迭代器范围内找到一个值,返回第一个位置,找不到则返回末尾。使用参数occurs表明
vector<int>::const_iterator find_val(vector<int>::const_iterator beg, vector<int>::const_iterator end, int value, vector<int>::size_type & occurs)
{
vector<int>::const_iterator res_iter = end;
occurs = 0;
for (; beg != end; ++beg)
{
if (*beg == value)
{
if (res_iter == end) // 若找到值,将返回的迭代器指向第一个位置
res_iter = beg;
++occurs;
}
}
return res_iter;
}

引用返回左值

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>
#include <string>

using std::cin;
using std::cout;
using std::endl;
using std::string;

// 返回引用
char &get_val(string &str, string::size_type ix)
{
return str[ix];
}

int main()
{
string s("hello");
cout << s << endl;
get_val(s, 1) = 'E'; // 对引用赋值

cout << s << endl;
return 0;
}

参数默认值

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
#include <iostream>
#include <string>
#include <sstream>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::ostringstream;

string screenInit(string::size_type hight = 20,
string::size_type width = 30); // 参数默认值

int main()
{
cout << "scree is " << screenInit(50);

return 0;
}

string screenInit(string::size_type hight, string::size_type width)
{
ostringstream strm;
strm << "hight:" << hight << " and width: " << width << endl; // 类型转换
return strm.str();
}

内联函数避免函数调用开销,放入头文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::endl;
using std::string;

// 内联函数避免函数调用开销,放入头文件
inline const string & shorterString(const string &s1, const string &s2)
{
return s1.size() < s2.size() ? s1 : s2;
}

int main()
{
cout << shorterString("aa", "bbb") << endl;
return 0;
}

类的成员函数

Dog.h

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
#pragma once
#include <iostream>
#include <string>

using std::string;

class Dog {
public: // 构造方法
Dog(const string name)
{
Dog::name = name;
}
Dog(): name("ss"),age(1) {} //构造函数初始化列表
public: // 公共成员函数
string get_name() const; // 返回值类型为const
int get_age() { return this->age; }
void set_age(int age) { this->age = age; }
void to_string() const;
private: // 私有成员变量
string name;
int age;
};

inline // 共有成员函数的内联实现
string Dog::get_name() const
{
return Dog::name;
}

main.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include "Dog.h"

using std::cin;
using std::cout;
using std::endl;
void Dog::to_string() const
{
cout << "dog name : " << this->get_name() << ",age :" << this->age << endl;
}
int main()
{
Dog dog; // 默认构造方法初始化
dog.to_string();
Dog tom("tom");
tom.set_age(18);
tom.to_string();
return 0;
}

指向函数的指针

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
#include <iostream>
#include <string>

using std::string;
using std::cout;
using std::endl;

int lengthCompare(const string &, const string &);

int(*ff(int))(int*, int); // 返回指向函数的指针 的函数调用

int f(int *, int);

int main()
{
// 使用typedef 简化函数指针使用
typedef int(*compFcn)(const string &, const string &);
compFcn pf = lengthCompare;
int flag = lengthCompare("hi", "hello");
cout << "compare result : " << flag << endl;
flag = pf("hello", "hello");
cout << "compare result : " << flag << endl;
flag = (*pf)("hello", "hi");
cout << "compare result : " << flag << endl;
int i = 1;
int(*fp)(int*, int) = ff(i); // 返回指向函数的指针 的函数调用
int r = fp(&i, i); // 函数指针调用
cout << "最终结果:" << r << endl;
cout << "最终结果,一步到位:" << ff(i)(&i,i) << endl;
return 0;
}

int lengthCompare(const string &s1, const string &s2)
{
return s1.size() - s2.size();
}

int(*ff(int a))(int * p, int i)
{
cout << "用参数a做点什么:" << a << endl;
int(*fp)(int*, int) = f; // 返回函数指针
return fp;
}

int f(int * p, int i)
{
return (*p) + i;
}

C++ 06 语句

简单语句

分号结束

复合语句

代码块,判断、循环等待

语句作用域

语句内部

if、switch、default、while、for、do-while、break、continue、goto

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using std::cin;
using std::cout;
using std::endl;

int main()
{
int num;
while (cin >> num)
{
if (num == 886) break;
if (num == 520) continue;
if (num == 69) goto ok;
if (num % 2 == 0)
cout << num << " is 偶数 " << endl;
else
cout << num << " is 奇数 " << endl;

}
ok:
cout << num << " is ok " << endl;
return 0;
}

异常处理 throw、try、catch

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
#include <iostream>

using std::cin;
using std::cout;
using std::endl;

double divide(int n1, int n2);

int main()
{
try
{
double n3 = divide(1, 0);
cout << "相除得:" << n3 << endl;
}
catch (std::runtime_error err)
{
cout << "相除异常:" << err.what() << endl;
}
}

double divide(int n1, int n2)
{
if (n2 == 0) throw std::runtime_error("被除数不能为0!");
return (double)n1 / n2;
}

C++ 05 表达式

算术表达式

加减乘除、取商取余数

关系操作符和逻辑表达啥

比较操作、逻辑判断

位操作符

取反,左右移,与或非

bitset对象

设置位,重置位,取位值:

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
#include <iostream>
#include <bitset>

using std::cout;
using std::endl;
using std::bitset;

int main()
{
bitset<4> bitset_quizl;
unsigned long int_quizl = 0;
cout << "bitset:" << bitset_quizl << "int_quizl:" << int_quizl << endl;

bitset_quizl.set(2); // 将第n位置为1
int_quizl |= 1UL << 2; // 两个操作等价,移位后位或操作,将该位置为1
cout << "bitset:" << bitset_quizl << "int_quizl:" << int_quizl << endl;

bitset_quizl.reset(2); // 将第n位置为0
int_quizl &= ~(1UL << 2); // 两个操作等价,移位后位取反再或操作,将该位置为0
cout << "bitset:" << bitset_quizl << "int_quizl:" << int_quizl << endl;

bool status1= bitset_quizl[2]; // 获取第n位的状态
bool status2 = int_quizl & (1UL << 2); // 使用掩码取值
cout << "bitset status: " << status1 << "int_quizl status :" << status2;
return 0;
}

移位操作符之于IO

重载之后,输入输出。

赋值操作符

从右向左,优先级低,赋值表达式等于左值。

自增自减操作符

前置先操作,后置后操作

箭头操作符

通过指针调用。

条件操作符

三目运算

sizeof操作符

返回对象或类型名长度,返回类型size_t

逗号操作符

隔开

复合表达式求值

优先级、结合性、求值顺序

new和delete表达式

new出来的,迟早都要delete

类型转换

C++ 04 数组和指针

数组、指针、动态数组

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
#include <iostream>
#include <string>
#include <bitset>

using std::cin;
using std::cout;
using std::endl;
using std::string;

int main()
{
// 显示初始化 , 数字长度为固定值
const unsigned size = 3;
int ia[size] = { 0,1,2 };
// 使用下标遍历
for (size_t ix = 0; ix != size; ++ix)
cout << ix << ":" << ia[ix] << endl;
// 使用指针遍历
for (int *p = ia; p < ia + size; p++)
cout << p << ":" << *p << endl;
// 创建动态数组
int *pia = new int[10]; // array of 10 uninitialized ints
int *pia2 = new int[10](); //数组初始化 0
delete[] pia; // 动态空间释放
delete[] pia2;
return 0;
}

还有const、C字符串、二维数组。没啥好记的,用到时再来翻看。

C++ 03 标准库类型

命名空间的using声明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::endl;
using std::string;
int main()
{
string s;
cin >> s;
cout << s << endl;
return 0;
}

string对象

string输入输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::getline;
int main()
{
// string 对象初始化
string s1;
string s2(s1);
string s3("value");
string s4(3, 'c'); // "ccc"
// while (cin >> s1) cout << s1 << endl;
// 使用getline() 读取整行
string line;
while (getline(cin, line))
cout << line << endl;
return 0;
}

string对象操作

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
#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::getline;
int main()
{
string st("Hi boy");
// size()
cout << "The size of " << st << "is " << st.size()
<< " characters , including the newline" << endl;
// empty()
if (!st.empty())
cout << "string " << st << " is not empty" << endl;
// == < > <= >=
if (st == "Hi boy")
cout << "Two word is same" << endl;
// +
string s3 = st + " Hi girl";
cout << "string + is " << s3 << endl;
// string中字符
for (string::size_type ix = 0; ix != st.size(); ++ix)
st[ix] = '*';
cout << "usage of string::size_type , st is " << st << endl;

return 0;
}

字符处理操作 cctype

cctype

vector 容器类型

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
#include <iostream>
#include <string>
#include <vector>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;

int main()
{
// vector 初始化
vector<int> vals(10); // 10 个0
string word;
vector<string> text;
cin >> word;
text.push_back(word); // 添加元素
for (vector<string>::size_type ix = 0; ix != text.size(); ++ix) // 遍历
cout << ix << ":" << text[ix] << endl;
// 迭代器
for (vector<string>::iterator iter = text.begin(); iter != text.end(); iter++)
*iter = "x";
// 不可修改内容的迭代器
for (vector<string>::const_iterator iter = text.begin(); iter != text.end(); iter++)
cout << *iter << endl;
return 0;
}

bitset 位操作

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
#include <iostream>
#include <string>
#include <bitset>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::bitset;

int main()
{
// vector 初始化
bitset<32> bitvec; // 32bits , all zero
bitset<16> bitvec1(0xffff); //0...15 are set to 1 一个16进制数,
string strval("1100");
bitset<32> bitvec4(strval); // 1100
string str("11111110000000011");
bitset<32> bitvec5(str, 5, 4); // 4 bits start str[5] , 1100
// bitset对象操作
bool is_set = bitvec.any(); // false
bool is_not_set = bitvec.none(); // true

size_t size = bitvec.size(); // 32
for (int index = 0; index != size; index += 2)
bitvec.set(index); // bitvec[index] = 1;

if (bitvec.test(0));
if (bitvec[0]);

bitvec.reset(); // all to 0
bitvec.set(); // all to 1

bitvec.flip(0);// 反转第一位
bitvec[0].flip();
bitvec.flip(); // 反转全部

bitset<128> bitvec3(0xffff);
unsigned long ulong = bitvec3.to_ullong(); // 取值与 二进制位输出
cout << "bitvec3 = " << ulong << " = " << bitvec3 << endl;
return 0;
}

C++ 02 变量和基本类型

基本内置类型

  • bool 布尔值
  • char 字符型 8位
  • wchar_t 宽字符型 16位
  • short 短整形 16位
  • int 整形 16位
  • long 长整型 32位
  • float 单精度浮点型 6位有效数字
  • double 双精度浮点型 10位有效数字
  • long double 扩展精度浮点型 10位有效数字

变量初始化

1
2
int ival(1024); // 直接初始化
int ival2 = 1024; // 复制初始化

const 限定符

修饰变量不可被修改,默认为文件的局部变量。
仅允许const引用绑定到需要临时使用的值:const int &r = 1024;

其他

类定义、头文件。。。
似曾相识,不写了。