c++ STL中Algorithm


#include <iostream>
#include <algorithm>
#include <string.h>
#include <sstream>
#include <numeric>      // std::accumulate
#include <vector>
#include <iterator>
#include <list>
#include <fstream>
using namespace std;


int main(int argc,char* argv[])
{
	//transform
	string s("heLLo");
	transform(s.begin(), s.end(), s.begin(), ::toupper);
	cout << s << endl;
	transform(s.begin(), s.end(), s.begin(), ::tolower);
	cout << s << endl;

	//count 统计单个字符出现的次数
	char s1[]="hello stl android stl fuck stl to baidu";
	int n=count(s1,s1+strlen(s1),'l');
	cout<<n<<endl;

	//erase
	string s2("   hello   ");
	s2.erase(0, s2.find_first_not_of(" \n"));
	cout << s2 << endl;
	s2.erase(s2.find_last_not_of(' ') + 1);
	cout << s2 << endl;


	//trim
	string s3("   hello, world. say bye   ");
	s3.erase(remove_if(s3.begin(),s3.end(), bind2nd(equal_to<char>(), ' ')), 
	s3.end());
	cout << s3 << endl;

	//replace 一次
	string s4("hello, world,hello,android");
	string sub("ello");
	s4.replace(s4.find(sub), sub.size(), "appy ");
	cout << s4 << endl;

	//startwith endwith
	string s5("hello, world");
	string head("hello");
	string tail("ld");
	bool startwith = s5.compare(0, head.size(), head) == 0;
	cout << boolalpha << startwith << endl;
	bool endwith = s5.compare(s5.size() - tail.size(), tail.size(), tail) == 0;
	cout << boolalpha << endwith << endl;

	string s6("123");
	int i = atoi(s6.c_str());
	cout << i << endl;
	int ii;
	stringstream(s6) >> ii;
	cout << ii << endl;

	string sd("12.3");
	double d = atof(sd.c_str());
	cout << d << endl;

	double dd;
	stringstream(sd) >> dd;
	cout << dd << endl;

	string sb("true");
	bool b;
	stringstream(sb) >> boolalpha >> b;
	cout << boolalpha << b << endl;

	//split
	/*string s7("hello world, bye.");
	vector<string> vect;
	vect.assign(
		istream_iterator<string>(stringstream(s7)),istream_iterator<string>(" ")
		);*/

	//concat strcat
	vector<string> vect;
	vect.push_back("hello");
	vect.push_back(", ");
	vect.push_back("world");
	string result= accumulate(vect.begin(), vect.end(), string(""));
	cout <<result<<endl;

	

	std::reverse(result.begin(), result.end());
	cout <<result<<endl;

	//逆转赋值
	string s8;
	s8.assign(result.rbegin(), result.rend());
	cout <<s8<<endl;

	//filename
	std::string filename("hello.exe");
	std::string::size_type pos = filename.rfind('.');
	std::string ext = filename.substr(pos == std::string::npos ? filename.length() : pos + 1);
	cout <<ext<<endl;


	int dist;
	list<int> col;
	col.push_back(150);
	col.push_back(14);
	col.push_back(145);
	col.push_back(11);
	col.push_back(15);
	col.push_back(123);
	col.push_back(187);
	col.push_back(17);
	col.push_back(4);

	std::list<int>::iterator it_pos = find(col.begin(), col.end(), 5);


	if ( it_pos != col.end())
		dist = std::distance(col.begin(), it_pos);
	cout <<dist<<endl;

	cout<<*max_element(col.begin(), col.end())<<endl;
	cout<<*min_element(col.begin(), col.end())<<endl;

	/*从文件中读取一行
	string input;
	input.reserve(1000);
	ifstream ifs("/tmp/tmp.txt");
	getline(ifs, input);
	cout<<input<<endl;*/

	//读取整个文件
	ifstream ifs("/tmp/tmp.txt");
	/*
	string input2(
		istreambuf_iterator<char>(ifs.rdbuf()), 
		istreambuf_iterator<char>()
		);
	cout<<input2<<endl;*/

	string input;
	input.reserve(10000);
	input.assign(
		istreambuf_iterator<char>(ifs.rdbuf()), 
		istreambuf_iterator<char>()
		);
	cout<<input<<endl;

	//copy
	ifstream ifs2("in.txt"); 
	ofstream ofs2("out.txt"); 
	ofs2 << ifs2.rdbuf(); 

	cin>>s;
	return 0;
}

template<typename InputIterator, typename OutputIterator, typename Predicate>
OutputIterator copy_if(
	InputIterator begin, InputIterator end, OutputIterator destBegin, Predicate p)
{
	while (begin != end) 
	{
		if (p(*begin))*destBegin++ = *begin;
		++begin;
	}
	return destBegin;
}
http://www.cplusplus.com/reference/algorithm/
https://msdn.microsoft.com/zh-cn/library/yah1y2x8.aspx

http://blog.csdn.net/woaifen3344/article/details/8050327


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值