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

}

 

目录
相关文章
|
1月前
|
存储 人工智能 Python
[oeasy]python061_如何接收输入_input函数_字符串_str_容器_ 输入输出
本文介绍了Python中如何使用`input()`函数接收用户输入。`input()`函数可以从标准输入流获取字符串,并将其赋值给变量。通过键盘输入的值可以实时赋予变量,实现动态输入。为了更好地理解其用法,文中通过实例演示了如何接收用户输入并存储在变量中,还介绍了`input()`函数的参数`prompt`,用于提供输入提示信息。最后总结了`input()`函数的核心功能及其应用场景。更多内容可参考蓝桥、GitHub和Gitee上的相关教程。
16 0
|
3月前
|
C语言 C++ 容器
【c++丨STL】string模拟实现(附源码)
本文详细介绍了如何模拟实现C++ STL中的`string`类,包括其构造函数、拷贝构造、赋值重载、析构函数等基本功能,以及字符串的插入、删除、查找、比较等操作。文章还展示了如何实现输入输出流操作符,使自定义的`string`类能够方便地与`cin`和`cout`配合使用。通过这些实现,读者不仅能加深对`string`类的理解,还能提升对C++编程技巧的掌握。
144 5
|
3月前
|
存储 编译器 C语言
【c++丨STL】string类的使用
本文介绍了C++中`string`类的基本概念及其主要接口。`string`类在C++标准库中扮演着重要角色,它提供了比C语言中字符串处理函数更丰富、安全和便捷的功能。文章详细讲解了`string`类的构造函数、赋值运算符、容量管理接口、元素访问及遍历方法、字符串修改操作、字符串运算接口、常量成员和非成员函数等内容。通过实例演示了如何使用这些接口进行字符串的创建、修改、查找和比较等操作,帮助读者更好地理解和掌握`string`类的应用。
93 2
|
4月前
|
IDE Java 开发工具
Java“未封闭的 String 表达式”怎么解决
要解决Java中的“未封闭的 String 表示”问题,需检查并修正字符串字面量,确保每个字符串被正确地用双引号括起来。若字符串跨越多行,可使用字符串连接操作符(+)或引入文本块(JDK 13 及以上版本)。这能帮助避免语法错误,并使代码更整洁易读。
172 7
|
4月前
|
安全 C语言 C++
【C++篇】探寻C++ STL之美:从string类的基础到高级操作的全面解析
【C++篇】探寻C++ STL之美:从string类的基础到高级操作的全面解析
69 4
|
4月前
|
编译器 C语言 C++
【C++】C++ STL 探索:String的使用与理解(三)
【C++】C++ STL 探索:String的使用与理解
|
4月前
|
存储 编译器 C++
【C++】C++ STL 探索:String的使用与理解(二)
【C++】C++ STL 探索:String的使用与理解
|
4月前
|
编译器 C语言 C++
【C++】C++ STL 探索:String的使用与理解(一)
【C++】C++ STL 探索:String的使用与理解
128 1
|
6月前
|
存储 算法 程序员
【STL】string
【STL】string
|
7月前
|
存储 语音技术 Python
语音识别,函数综合案例,黑马ATM,/t/t一个对不齐,用两个/t,数据容器入门,数据容器可以分为列表(list)、元组(tuple)、字符串(str)、集合(set)、字典(dict)
语音识别,函数综合案例,黑马ATM,/t/t一个对不齐,用两个/t,数据容器入门,数据容器可以分为列表(list)、元组(tuple)、字符串(str)、集合(set)、字典(dict)