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; });