- tuple
- bitset
- 随机数生成
- 正则表达式
- IO库再探
std::tuple<size_t, size_t, size_t> threeD{ 1,2,3 }; //直接初始化
auto user = std::make_tuple("hh", 160, 90.2); //生成tuple类型对象,根据初始值推断类型
//访问
auto username = std::get<0>(user); //尖括号内只能用整型常量表达式
std::cout << username << std::endl;
//tuple成员修改
std::get<0>(user) = "xx";
std::cout << std::get<0>(user) << std::endl;
//通过指针修改
auto* usernamep = &(std::get<0>(user));
* usernamep = "aa";
std::cout << std::get<0>(user) << std::endl;
//获得成员类型
typedef decltype(user) userstype;
std::tuple_element<1, userstype>::type cnt = std::get<1>(user); //int cnt,cnt类型与item中第二个成员相同
//获得成员数量
size_t sz = std::tuple_size<userstype>::value;
std::cout << sz << std::endl;
当对应运算符操作合法且两个tuple类型对象成员数量相同时,可以使用关系运算符和相等运算符进行比较操作。
#include
#include
#include
#include
typedef std::tuple<std::string, size_t, double> hhtype;
class hhdy{
public:
friend class hhdyvec;
hhdy(std::string s = "", size_t count = 0,double p= 0.0):name(s),cnt(count),price(p){}
std::string getname() { return this->name; }
hhtype get_tuple() { return std::make_tuple(name, cnt, price); }
private:
std::string name;
size_t cnt;
double price;
};
class hhdyvec {
public:
friend std::vector<hhtype> get_record_of_book(std::string& s, hhdyvec& hv);
hhdyvec() {};
hhdyvec(std::vector<hhdy> v):vech(v){}
hhdyvec& push_back(hhdy h) {
vech.push_back(h);
return *this;
}
private:
std::vector<hhdy> vech;
};
//返回hhdyvec中指定名字的tuple类型对象
std::vector<hhtype> get_record_of_book(std::string& s, hhdyvec& hv) {
std::vector<hhtype> ret;
for (hhdy h : hv.vech) {
if (h.getname() == s)
ret.push_back(h.get_tuple());
}
return ret;
}
//打印
std::ostream& print_record_of_book(std::istream& is,std::ostream& os, hhdyvec& hv) {
std::string s;
os << "输入书名: ";
is >> s;
std::vector<hhtype> ret = get_record_of_book(s, hv);
size_t st = std::tuple_size<hhtype>::value;
for (hhtype h : ret) {
os <<"bookname: "<<std::get<0>(h)
<<" count: "<<std::get<1>(h)
<<" price: "<<std::get<2>(h)
<< std::endl;
}
return os;
}
std::ostream& print_books(std::istream& is, std::ostream& os, hhdyvec& hv) {
std::string s;
do {
print_record_of_book(is, os, hv);
os << "continue ?(y/n)";
is >> s;
} while (s == "y" || s == "Y");
return os;
}
int main() {
hhdyvec hv;
hhdy h1 = hhdy("book1", 1, 10);
hhdy h2 = hhdy("book2", 2, 20);
hv.push_back(h1);
hv.push_back(h2);
hv.push_back(h2);
print_books(std::cin, std::cout, hv);
return 0;
}

#include| 定义 | 说明 |
|---|---|
bitset | n位,每位都是0,构造函数是constexpr |
bitset | u为unsigned long long类型,b是u的第n位拷贝。b超出的高位部分置0,构造函数是constexpr |
bitset; | string从下标pos开始m个字符的拷贝。字符串只包含zero和one。构造函数是explicit的 |
bitset | 从cp指向的字符数组中拷贝字符。构造函数是explicit的 |
//使用整型值来初始化bitset时,只会被转换为unsigned long long类型处理。
std::bitset<32> b1(0xaaaa);//00000000000000001010101010101010
std::bitset<10> b2(0xaaaa);//1010101010
//0ULL是64个0,~0ULL是64个1
std::bitset<128> b3(~0ULL);//0-63:1,64-127:0
//string下标最大的字符初始化bitset最低位(注意使用的位置)
std::bitset<10> bs1("1100"); //0000001100
std::string s = "111000";//左为下标最小(1的位置为下标0)
std::bitset<10> bs2(s,0,2); //0000000011
std::bitset<10> bs3(s, 2); //0000001000
std::bitset<10> bs4("22323333",6,'3','2'); //0000110100
| string | 1 | 1 | 0 | 0 |
|---|---|---|---|---|
| s[0] | s[1] | s[2] | s[3] | |
| bitset | 1 | 1 | 0 | 0 |
| b[3] | b[2] | b[1] | b[0] |
| 类别 | 操作 | 说明 |
|---|---|---|
| 置位判断 | b.any() | 是否存在置位 |
b.all() | 所有位置位为真 | |
b.none() | 所有位置不存在置位为真 | |
b.count() | 置位位数 | |
b.test(pos) | pos位置置位返回真 | |
| 访问 | b.size() | constexpr函数,返回b位数 |
b[pos] | 访问b中pos位置的位 | |
| 修改 | b.set(pos,v) | 设置pos处的位值为v,v默认true |
b.set() | 设置全部位值为v | |
b.reset(pos) | 复位 | |
b.reset() | ||
b.flip(pos) | 取反 | |
b.flip() | ||
| 转换 | b.to_ulong() | 无法放入时会抛出异常overflow_error,UL最长32位,ULL最长64位 |
b.to_ullong() | ||
b.to_string(zero,one) | ||
| 打印 | os< | |
is>>b |
std::cout <<"bitset操作:" << std::endl;
std::bitset<64> b5(0xaaaa);//1010101010101010
try {
auto ul = b5.to_ulong();
std::cout << ul << std::endl;
}catch (std::overflow_error err) {
std::cerr << err.what() << std::endl;
}
std::bitset<30> quizB;
quizB.set(27); //设置第27位是否通过测验
status = quizB[27]; //查看27未是否通过测验
quizB.reset(27);
空,代补
#include,库中包含随机数引擎类和随机数分布类,通过引擎类生成的随机数,使用分布类生成指定类型、给定范围内的、服从特定概率分布的随机数。