1multiset中用equal_range来遍历所有的元素
#include <set>
#include <iostream>
using namespace std;
//multiset中存储的元素是不重复的
void main()
{
multiset<int> myset;
myset.insert(100);
myset.insert(101);
myset.insert(100);
myset.insert(103);
myset.insert(100);
auto pfind = myset.find(101);
std::cout << *pfind << std::endl;
//找到红黑树的链表节点,遍历所有的元素
auto allfind = myset.equal_range(100);
//find链表的头结点,second最后一个空节点,遍历所有的元素
for (auto it = allfind.first; it != allfind.second; it++)
{
cout << *it << endl;
}
cin.get();
}
运行结果是:
2multimap中的equal_range
#include <iostream>
#include <map>
#include <string>
using namespace std;
void main()
{
multimap<string, string> mymap;
mymap.insert(pair<string, string>("yincheng", "a"));
mymap.insert(pair<string, string>("yincheng1", "b"));
mymap.insert(pair<string, string>("yincheng", "c"));
mymap.insert(pair<string, string>("yincheng", "d"));
cout << "--正向迭代输出的结果--" << endl;
auto ib = mymap.begin();
auto ie = mymap.end();
for (; ib != ie; ib++)
{
cout << (*ib).first << " " << (*ib).second << endl;
}
auto pfind = mymap.find("yincheng");
cout << "\n\n\n";
cout << "---正向迭代输出结束---" << endl;
cout << (*pfind).first << " " << (*pfind).second << endl;
cout << "\n\n\n";
//从树节点把关键字相同的链表全部拨下
auto it = mymap.equal_range("yincheng");
cout << "---输出equal_range()得出的结果---" << endl;
//first起点,secondl链表最后的节点后面一个空节点,都是迭代器
for (auto i = it.first; i != it.second; i++)
{
cout << (*i).first << " " << (*i).second << endl;
}
cin.get();
}
运行结果:
3bitset容器
案例1
#include <set>
#include <bitset>
#include <iostream>
#include <string>
using namespace std;
void main()
{
//8位,(215)代表构造的数据
bitset<8>bs(255);
//最高位存储i=7
for (int i = 0; i < 8;i++)
{
cout << bs[i];
}
cin.get();
}
运行结果:
案例2:(取出整数的每位和取反的位)
#include <set>
#include <bitset>
#include <iostream>
#include <string>
using namespace std;
void main()
{
//8位,(215)代表构造的数据
bitset<8>bs(215);
for (int i = 7; i >= 0;i--)
{
cout << bs[i] << " " << ~bs[i] << endl;
}
cin.get();
}
运行结果:
案例3:(取出浮点数的每位)
#include <set>
#include <bitset>
#include <iostream>
#include <string>
using namespace std;
void main()
{
float num = 1231231236.8;
bitset<32> myset(num);
for (int i = 31; i >= 0;i--)
{
cout << myset[i];
}
cin.get();
}
运行结果:
案例4:
将bitset中的结果打印成二进制的数据
#include <set>
#include <bitset>
#include <iostream>
#include <string>
using namespace std;
void main()
{
int num = -5;
bitset<32> myset(num);
for (int i = 31; i >= 0;i--)
{
cout << myset[i];
}
cout << "\n--打印出字符串类型的结果--";
string str = myset.to_string();
cout << "\n" << str << endl;
cin.get();
}
运行结果:
案例5(设置指定位为0):
#include <set>
#include <bitset>
#include <iostream>
#include <string>
using namespace std;
void main()
{
bitset<8> bs(255);
bs.set(7, 0);//操作二进制位,将最后一位设置成为0
bs.set(0, 0);//将第一位设置成为0
cout << bs.size() << endl;//位数
//bs.reset();//全部清零
//bs.none();//测试下是否有越位
//最高位存储i=7上
for (int i = 7; i >= 0;i--)
{
cout << bs[i];
}
cin.get();
}
运行结果:
4.字符串操作
案例1(字符串赋值):
#include<string>
#include <iostream>
#include <stdlib.h>
using namespace std;
void main()
{
char str[124] = "china is big";
//str = "12321";//C写法
//string str1(str);
//str1 = "china is great";
string str1("ABCDEFG");
std::cout << str1 << "\n";
str1 = "china is china";
std::cout << str1;
cin.get();
}
案例2(字符串相加):
#include<string>
#include <iostream>
#include <stdlib.h>
using namespace std;
void main()
{
string str1("ABCD");
string str2("1234");
string str3 = str1 + str2;
cout << str3;
//下面两种不能通过字符串相加的方式实现
char stra[12] = "1231";
char strb[24] = "2344";
cin.get();
}
运行结果:
案例3:字符串追加
#include<string>
#include <iostream>
#include <stdlib.h>
using namespace std;
void main()
{
string str1("ABCD");
string str2("1234");
str1.append(str2);//字符串的增加
std::cout << str1;
cin.get();
}
运行结果:
案例4:字符任意位置插入
#include<string>
#include <iostream>
#include <stdlib.h>
using namespace std;
void main()
{
string str1("ABCD");
string str2("1234");
//任意位置插入字符,首部插入X
str1.insert(str1.begin(), 'X');
//尾部插入字符
str1.insert(str1.end(), 'X');
std::cout << str1;
cin.get();
}
运行结果:
案例5:字符串删除相关
#include<string>
#include <iostream>
#include <stdlib.h>
using namespace std;
void main()
{
string str1("12345678");
auto ib = str1.begin();
auto ie = str1.end();
for (; ib != ie; ib++)
{
cout << *ib << endl;
}
//str1.erase(str1.begin());//删除一个字符
//str1.erase(str1.begin()+3,str1.end()-2);//删除某个字符串
str1.erase(3, 4);//c从第三个字符开始删除四个字符
cout << str1 << endl;
cin.get();
}
运行结果:
案例6,字符串替换
#include<string>
#include <iostream>
#include <stdlib.h>
using namespace std;
void main()
{
string str1("12345678china");
//从0到第三个字符替换为china
//从第5个位置开始,替换第5个位置开始后的3个字符为china
str1.replace(5, 3, "china");
cout << str1 << endl;
cin.get();
}
运行结果:
案例6(字符串查找):
#include<string>
#include <iostream>
#include <stdlib.h>
using namespace std;
void main()
{
string str("233锄禾日当午,谭胜把地雷买下土,谭胜来跳舞,炸成250");
//-1表示没有找到
cout << (int)str.find("谭胜大爷") << endl;
int pos = str.find(",");//找到第一个匹配的,不匹配返回-1
cout << pos << endl;
pos = str.rfind("谭胜");//找到第一个匹配
cout << pos << endl;
pos = str.find("谭胜");
cout << pos;
cin.get();
}
运行结果:
案例7:字符串查找
#include<string>
#include <iostream>
#include <stdlib.h>
using namespace std;
void main()
{
string str("ab123mn");
//find_firstof是第一个找到与字符串匹配字符位置
int pos = str.find_first_of("123");
cout << pos << endl;
//find_first_not_of是找到第一个与字符串不匹配字符位置
pos = str.find_first_not_of("abc");
cout << pos << endl;
//find_last_of找到最后一个与字符串匹配字符位置
pos = str.find_last_of("123");
cout << pos << endl;
//find_last_not_of是从最后找到与字符串不匹配的位置
pos = str.find_last_not_of("123");
cout << pos << endl;
cin.get();
}
运行结果:
案例8:字符串比较
#include<string>
#include <iostream>
#include <stdlib.h>
using namespace std;
void main()
{
string str1 = "calc";
string str2 = "ABC1";
char strA[5] = "Asd";
char strB[5] = "Asd";
cout << (str1 == str2) << endl;//重载了运算符
cout << (strA == strB) << endl;//比较地址
cout << str1.empty() << endl;////是否为空
const char *p = str1.c_str();
system(p);
cin.get();
cin.get();
}
运行结果:
案例9:从指定位置开始查找字符或字符串
#include<string>
#include <iostream>
#include <stdlib.h>
using namespace std;
void main()
{
string str("abc is abc china is china");
int pos = str.find('a', 0);//字符也一样
std::cout << pos << endl;
pos = str.find('a', pos + 1);
std::cout << pos << endl;
pos = str.find("abc", 0);//find从指定位置开始查找元素
std::cout << pos << endl;
pos = str.find("abc", pos + 3);
std::cout << pos << endl;
cin.get();
}
运行结果:
5. R表达式,也叫lambda表达式
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
//模板类,实现对某些容器元素的操作
template<class T>
class add
{
public:
//重载()运算符,进行操作
void operator()(T &t)
{
t *= 2;
std::cout << t << "\n";
}
};
void go(int a)
{
a *= 2;
std::cout << a << "\n";
}
void main()
{
vector<int> myv;
myv.push_back(10);
myv.push_back(9);
myv.push_back(7);
myv.push_back(9);
add<int> addA;//省略
for_each(myv.begin(), myv.end(), addA);
cout << "-----------" << endl;
//和上面的等价,调用重载的()
for_each(myv.begin(), myv.end(), add<int>());
cout << "-----------" << endl;
for_each(myv.begin(), myv.end(), go);
cout << "-----------" << endl;
//R表达式,也叫lambda表达式
auto fun = [](int a, int b){return a + b; };
auto funA = [](int a){a *= 2; cout << a << endl; };
cout << fun(1, 2) << endl;
for_each(myv.begin(), myv.end(), funA);
for_each(myv.begin(), myv.end(), [](int a){a *= 2; cout << a << endl; });
cin.get();
}