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

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
简介:  1multiset中用equal_range来遍历所有的元素 #include <set> #include <iostream> using namespace std;   //multiset中存储的元素是不重复的 void main() {     multiset<int>


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

}

 

目录
相关文章
|
21小时前
string(字符串)
在 Lua 中,字符串可以用双引号或单引号定义,如 `string1 = &quot;this is string1&quot;` 和 `string2 = &#39;this is string2&#39;`。多行字符串可由两个方括号包围,例如 `html` 变量所示,它包含了一个 HTML 片段。Lua 会尝试将数字字符串转换为数值进行算术运算,但混合字符串和数字可能导致错误,如 `&quot;error&quot; + 1`。
|
21小时前
|
存储 设计模式 算法
【C++/STL】stack和queue(容器适配器、优先队列、双端队列)
【C++/STL】stack和queue(容器适配器、优先队列、双端队列)
14 1
|
21小时前
|
存储 算法 C++
详解C++中的STL(标准模板库)容器
【4月更文挑战第30天】C++ STL容器包括序列容器(如`vector`、`list`、`deque`、`forward_list`、`array`和`string`)、关联容器(如`set`、`multiset`、`map`和`multimap`)和容器适配器(如`stack`、`queue`和`priority_queue`)。它们为动态数组、链表、栈、队列、集合和映射等数据结构提供了高效实现。选择合适的容器类型可优化性能,满足不同编程需求。
|
21小时前
|
C++ 容器
STL—map容器
STL—map容器
|
21小时前
|
缓存 安全 Java
【Java基础】String、StringBuffer和StringBuilder三种字符串对比
【Java基础】String、StringBuffer和StringBuilder三种字符串对比
9 0
|
21小时前
|
存储 算法 程序员
C++从入门到精通:2.2.1标准库与STL容器算法深度解析
C++从入门到精通:2.2.1标准库与STL容器算法深度解析
|
21小时前
|
存储 编译器 Linux
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
|
21小时前
|
C++ 容器
约瑟夫经典问题C++,STL容器queue解法
约瑟夫经典问题C++,STL容器queue解法
14 0
|
21小时前
|
索引 容器
06-数据容器str(字符串)-字符串的下标索引/字符串无法修改/查找字符串下标初始值/字符串的替换/字符串的分割/字符串去除前后空格/统计字符串的数量/字符串的循环遍历/对字符串进行分割
06-数据容器str(字符串)-字符串的下标索引/字符串无法修改/查找字符串下标初始值/字符串的替换/字符串的分割/字符串去除前后空格/统计字符串的数量/字符串的循环遍历/对字符串进行分割
|
21小时前
|
JavaScript
js 字符串String转对象Object
该代码示例展示了如何将一个以逗号分隔的字符串(`&#39;1.2,2,3,4,5&#39;`)转换为对象数组。通过使用`split(&#39;,&#39;)`分割字符串并`map(parseFloat)`处理每个元素,将字符串转换成浮点数数组,最终得到一个对象数组,其类型为`object`。