【STL】根据一段代码总结STL中vector的一些使用方法

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

//输出字符串
void OutputString(string str)
{
	cout<<str<<endl;
}

//判断字符串是不是world
bool IsWorld(string& str)
{
	if(str == "world")
	{
		return true;
	}
	return false;
}

void main()
{
	cout<<"OK,JUST DO IT!!"<<endl;
	vector<string> strA;
	strA.push_back("I am the first string!");
	strA.push_back("I am the second string!");
	strA.push_back("I am the third string !");
	strA.push_back("hello");
	strA.push_back("world");
	strA.push_back("hello");
	strA.push_back("hello");
	strA.push_back("world");
	
	//1.用for循环输出vector中所有的字符串
	cout<<"for research strA---------------------------------"<<endl;
	for( vector<string>::iterator it = strA.begin(); it != strA.end(); it++ )
	{
		cout<<*it<<endl;
	}

	//2.for_each输出所有的字符串
	cout<<"for_each research strA----------------------------"<<endl;
	for_each(strA.begin(),strA.end(),OutputString);

	string dis = "hello";
	//判断vector中字符串的数目
	int num = count( strA.begin(), strA.end(), dis );
	cout<<"there are "<<num<<" of \"hello\" string!"<<endl;

	//第三个参数可以是一个函数,
	//还可以是一个类(含有operator),
	//要求返回值要求均为bool型
	num = count_if( strA.begin(), strA.end(), IsWorld );
	cout<<"there are "<<num<<" of \"world\" string!"<<endl;

	string strWorld = "world";
	//find前两个参数是iterator(迭代器),表示查找半闭合区间的前后两个位置
	//第三个参数是于strA容器中相匹配的数据类型的一个对象
	vector<string>::iterator itWorld = find( strA.begin(), strA.end(), strWorld);
	if( itWorld != strA.end() )
	{
		cout<<"find "<<*itWorld<<endl;
	}
	else
	{
		cout<<"not find!"<<endl;
	}

	itWorld = find_if( strA.begin(), strA.end(), IsWorld );
	if( itWorld != strA.end() )
	{
		cout<<"find_if "<<*itWorld<<endl;
	}
	else
	{
		cout<<"not find_if!"<<endl;
	}

	vector<string> strSearch;
	strSearch.push_back("hello");
	strSearch.push_back("world");
	//search()算法,它是搜索的一个范围,如果成功返回一个指着strA中第一个于strSearch匹配的iterator,否侧返回strA的end()
	//find和find_if算法查找的是容器的第一个匹配的值
	vector<string>::iterator itSearch = search( strA.begin(), strA.end(), strSearch.begin(), strSearch.end() );
	if( itSearch != strA.end() )
	{
		cout<<"search success!"<<endl;
	}
	else
	{
		cout<<"search fail!"<<endl;
	}
	//sort()算法只使用于可以随机访问的类型,vector可以,list不可以,他有自己的sort算法
	sort(strA.begin(),strA.end());
	cout<<"after sort ----------------------------------------"<<endl;
	for_each(strA.begin(),strA.end(),OutputString);
	
	cin.get();
	cin.get();
}


1.定义一个含有字符串的vector: vector<string> strA
2.push_back将数据压进vector:  strA.push_back("hello world")
3.定义一个it指示器:vector<string>::iterator it 
4.for_each输出vector中的数据:for_each(strA.begin(),strA.end(),foreach)
5.count ,count_if统计vector中某个字符的数目:
num = count( strA.begin(), strA.end(), dis )
num = count_if( strA.begin(), strA.end(), IsWorld )
第三个参数可以是一个函数,还可以是一个类(含有operator),要求返回值要求均为bool型
6.find 和 find_if的返回值是一个iterator 
find的第三个参数是一个string,find_if的第三个参数是一个function或者class
vector<string>::iterator  itWorld = find( strA.begin(), strA.end(), strWorld)
vector<string>::iterator  itWorld = find_if( strA.begin(), strA.end(), IsWorld )
7.search算法搜索的是一个范围,find是一个值
search如果成功返回一个指着strA中第一个于strSearch匹配的iterator,否侧返回strA的end()
8.sort算法只使用于可以随机访问的类型,vector可以,list不可以,他有自己的sort算法
sort(strA.begin(),strA.end());

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值