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