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

}

 

时间: 2024-09-16 17:43:35

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

探索Java语言与JVM中的Lambda表达式

Lambda表达式是自Java SE 5引入泛型以来最重大的Java语言新特性,本文是2012年度最后一期Java Magazine中的一篇文章,它介绍了Lamdba的设计初衷,应用场景与基本语法.(2013.01.07最后更新) Lambda表达式,这个名字由该项目的专家组选定,描述了一种新的函数式编程结构,这个即将出现在Java SE 8中的新特性正被大家急切地等待着.有时你也会听到人们使用诸如闭包,函数直接量,匿名函数,及SAM(Single Abstract Method)这样的术语.其

C++11新特性中的匿名函数Lambda表达式的汇编实现分析(一)

Constructs a closure: an unnamed function object capable of capturing variables in scope. -- Lambda functions (since C++11) [cppreference.com] 按照C++11标准的说法,lambda表达式的标准格式如下: [ capture ] ( params ) mutable exception attribute -> ret { body } // (1) 完整

Java编程中使用lambda表达式的奇技淫巧_java

为什么使用Lambda表达式先看几个例子: 第一个例子,在一个独立的线程中执行某项任务,我们通常这么实现: class Worker implements Runnable { public void run() { for (int i = 0; i < 100; i++) doWork(); } ... } Worker w = new Worker(); new Thread(w).start(); 第二个例子,自定义字符串比较的方法(通过字符串长度),一般这么做: class Lengt

从.NET中委托写法的演变谈开去(中):Lambda表达式及其优势

在上一篇文章中我们简单探讨了.NET 1.x和.NET 2.0中委托表现形式的变化,以及.NET 2.0中匿名方法的优势.目的及注意事项.那么现在我们来谈一下.NET 3.5(C# 3.0)中,委托的表现形式又演变成了什么样子,还有什么特点和作用. .NET 3.5中委托的写法(Lambda表达式) Lambda表达式在C#中的写法是"arg-list => expr-body","=>"符号左边为表达式的参数列表,右边则是表达式体(body).参数列表

Android 中Lambda表达式的使用实例详解

Android 中Lambda表达式的使用实例详解 Java8 中着实引入了一些非常有特色的功能,如Lambda表达式.streamAPI.接口默认实现等等.Lambda表达式在 Android 中最低兼容到 Android2.3 系统,兼容性还是不错的,Lambda表达式本质上是一种匿名方法,它既没有方法名,也没有访问修饰符和返回值类型,使用它编写的代码将更加简洁易读. 1.Lambda表达式的基本写法 如果想要在 Android 项目中使用 Lambda表达式 或者 Java8 的其他新特性

一起谈.NET技术,从.NET中委托写法的演变谈开去(中):Lambda表达式及其优势

在上一篇文章中我们简单探讨了.NET 1.x和.NET 2.0中委托表现形式的变化,以及.NET 2.0中匿名方法的优势.目的及注意事项.那么现在我们来谈一下.NET 3.5(C# 3.0)中,委托的表现形式又演变成了什么样子,还有什么特点和作用. .NET 3.5中委托的写法(Lambda表达式) Lambda表达式在C#中的写法是"arg-list => expr-body","=>"符号左边为表达式的参数列表,右边则是表达式体(body).参数列表

实例讲解C++编程中lambda表达式的使用_C 语言

函数对象与Lambdas你编写代码时,尤其是使用 STL 算法时,可能会使用函数指针和函数对象来解决问题和执行计算.函数指针和函数对象各有利弊.例如,函数指针具有最低的语法开销,但不保持范围内的状态,函数对象可保持状态,但需要类定义的语法开销. lambda 结合了函数指针和函数对象的优点并避免其缺点.lambda 与函数对象相似的是灵活并且可以保持状态,但不同的是其简洁的语法不需要显式类定义. 使用lambda,相比等效的函数对象代码,您可以写出不太复杂并且不容易出错的代码. 下面的示例比较l

结合C++11新特性来学习C++中lambda表达式的用法_C 语言

在 C++ 11 中,lambda 表达式(通常称为 "lambda")是一种在被调用的位置或作为参数传递给函数的位置定义匿名函数对象的简便方法. Lambda 通常用于封装传递给算法或异步方法的少量代码行. 本文定义了 lambda 是什么,将 lambda 与其他编程技术进行比较,描述其优点,并提供一个基本示例.Lambda 表达式的各部分ISO C++ 标准展示了作为第三个参数传递给 std::sort() 函数的简单 lambda: #include <algorithm

C++中的Lambda表达式详解_C 语言

我是搞C++的 一直都在提醒自己,我是搞C++的:但是当C++11出来这么长时间了,我却没有跟着队伍走,发现很对不起自己的身份,也还好,发现自己也有段时间没有写C++代码了.今天看到了C++中的Lambda表达式,虽然用过C#的,但是C++的,一直没有用,也不知道怎么用,就可怜的连Lambda语法都看不懂.好了,这里就对C++中的Lambda进行一个简单的总结,就算是对自己的一个交代,我是搞C++的,我是一个C++ programmer. 一段简单的Code 我也不是文艺的人,对于Lambda的