C++笔记

C++

mysql库

1
sudo apt-get install libmysqlclient-dev
1
#include <mysql/mysql.h>

libcurl库

C++ 用libcurl库进行http通讯网络编程

tuple

元组,相当于简单的数据结构

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 <cmath>
#include <string>
#include <set>
#include <algorithm>
#include <tuple>
#include <vector>

using namespace std;
tuple<int, string, vector<int>> mtuple()
{
vector<int> a{ 5,1 };
return make_tuple (2,"test2",a);
//return { 2,"hhhh",a }; //c++17
}
//std::tie可以用于比较
struct S {
int n;
std::string s;
float d;
bool operator<(const S& rhs) const
{
// 比较 n 与 rhs.n,
// 然后为 s 与 rhs.s,
// 然后为 d 与 rhs.d
return std::tie(n, s, d) < std::tie(rhs.n, rhs.s, rhs.d);
}
};
int main()
{
tuple<int, string, vector<int>> test;
vector<int> a{ 1, 2, 3, 4, 5 };
test = make_tuple(1, "hello", a);
tuple<int, string, vector<int>> test1(1,"test",a);
cout << get<0>(test1) << " "<<get<1>(test1) ;
//std::tie 可以用于解包
set<int> b;
std::set<int>::iterator iter;
bool inserted;
tie(iter, inserted)=b.insert(3);

}

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


using namespace std;
void bitset_hash()
{
std::bitset<4> b1(1);
std::bitset<4> b2(2);
std::bitset<4> b3(b2);

std::hash<std::bitset<4>> hash_fn;

size_t h1 = hash_fn(b1);
size_t h2 = hash_fn(b2);
size_t h3 = hash_fn(b3);

std::cout << h1 << '\n';
std::cout << h2 << '\n';
std::cout << h3 << '\n';
}
int main()
{
string a = "110010";
bitset<8> b1;
bitset<8> b2(43);
bitset<8> b3("110010", 2);
bitset<8> b4(a, 2, 3);
b4.set(8);
}

正则表达式

inline函数不能分成头文件和实现文件

C和C++混合编程

1
2
3
4
5
6
7
8
9
10
//__cplusplus是内部定义的宏
#ifdef __cplusplus
extern "C" {
#endif

void *memset(void* ,int , size_t);

#ifdef __cplusplus
}
#endif

mutable

可变数据成员,即使在const函数内被mutable修饰的变量也可变

1
2
3
4
5
6
7
8
9
10
11
12
13
class test{
public:
test():times(0){}
void output() const;
private:
mutable int times;
}

void test::output() const
{
cout<<"output"<<endl;
times++;
}

explicit

禁止构造函数隐式转换