日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

STL之multiset中equal_range,multimap中的equal_range,bitset容器,string字符串操作,lambda表达式

發布時間:2024/9/27 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 STL之multiset中equal_range,multimap中的equal_range,bitset容器,string字符串操作,lambda表达式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.


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

}

?

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的STL之multiset中equal_range,multimap中的equal_range,bitset容器,string字符串操作,lambda表达式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。