C++ prime 第十章

这些代码片段展示了C++中使用标准库进行字符串处理、去重、排序和条件筛选的基本技巧,包括vector、list、algorithm和迭代器的运用。从查找特定值的出现次数到根据长度和大小进行定制排序,涉及了基本的数据结构和算法应用。
摘要由CSDN通过智能技术生成

10.1

#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
#include<vector>
#include<forward_list>
#include<deque>
#include<algorithm>

using namespace std;



int main(int argc, char** argv)
{
	int a[10] = { 0,1,2,5,4,5,4,5,4,5 };
	vector<int> vec(a, a + 10);
	int value = 5;
	cout << value << "出现的次数为:" << count(vec.begin(), vec.end(), value) << endl;

	return 0;
}

10.11

#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
#include<vector>
#include<forward_list>
#include<deque>
#include<algorithm>
#include<list>
using namespace std;

bool isShorter(const string& s1, const string& s2)
{
	return s1.size() < s2.size();


}

void elimDups(vector<string>& words)
{
	sort(words.begin(), words.end());   //按字典排序 
	auto end_unique = unique(words.begin(), words.end());  //把重复单词放置末尾  返回不重复区域之后的一个位置

	words.erase(end_unique, words.end());  // 删除重复区域

}

int main(int argc, char** argv)
{
	
	vector<string> words = { "the","quick","red","fox","jumps","over","the","slow","red","turtle" };
	elimDups(words);

	stable_sort(words.begin(), words.end(), isShorter);   //按长短排序  长度相同的单词 不打乱字典排序
	for (const auto &s : words)
	{
		cout << s << " ";
	}


	return 0;
}

10.13

#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
#include<vector>
#include<forward_list>
#include<deque>
#include<algorithm>
#include<list>
using namespace std;

bool cmp(const string& a)
{
	return a.size() >= 5;


}


int main(int argc, char** argv)
{
	
	vector<string> words = { "the","quick","red","fox","jumps","over","the","slow","red","turtle" };

	auto par = partition(words.begin(), words.end(), cmp);

	words.erase(par, words.end());

	for (auto &i : words)
	{
		cout << i << "  ";
	}


	return 0;
}

10.18

#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
#include<vector>
#include<forward_list>
#include<deque>
#include<algorithm>
#include<list>

using namespace std;

bool isShorter(const string& s1, const string& s2) {
	return s1.size() < s2.size();
}

void elimDups(vector<string>& words) {
	sort(words.begin(), words.end());
	auto end_unique = unique(words.begin(), words.end());
	words.erase(end_unique, words.end());
}

bool cmp(const string& a)
{
	return a.size() >= 4;

}

void biggis(vector<string>& s, vector<string>::size_type sz)
{
	elimDups(s);
	stable_sort(s.begin(), s.end(), [](const string& a, const string& b) {return a.size() < b.size(); });
	auto it = partition(s.begin(), s.end(), [sz](const string& s) {return s.size() <= sz; });
	for (it; it != s.end(); ++it)
	{
		cout << *it << "  ";
	}
}

int main(int argc, char** argv)
{
	
	vector<string> words = { "the","quick","red","fox","jumps","over","the","slow","red","turtle" };

	biggis(words, 4);

	/*auto par = partition(words.begin(), words.end(), cmp);*/

	/*words.erase(par, words.end());*/

	/*for (auto &i : words)
	{
		cout << i << "  ";
	}*/

	/*int a = 1;
	auto  f = [a](int b) {return a + b; };
	cout << f(2)  ;*/

	return 0;
}

10.25

#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
#include<vector>
#include<forward_list>
#include<deque>
#include<algorithm>
#include<list>
#include<functional>
#include<iterator>

using namespace std;

using namespace placeholders;

//bool isShorter(const string& s1, const string& s2) {
//	return s1.size() < s2.size();
//}

vector<string> elimDups(vector<string>& words) {
	sort(words.begin(), words.end());
	auto end_unique = unique(words.begin(), words.end());
	words.erase(end_unique, words.end());
	return words;
}
//
//bool cmp(const string& a)
//{
//	return a.size() >= 4;
//
//}
//
//void biggis(vector<string>& s, vector<string>::size_type sz)
//{
//	elimDups(s);
//	stable_sort(s.begin(), s.end(), [](const string& a, const string& b) {return a.size() < b.size(); });
//	auto it = partition(s.begin(), s.end(), [sz](const string& a, const string& b) {return a.size() < b.size(); });
//
//}
//bool judge_size(string& s, string::size_type sz)
//{
//	return s.size() <= sz;
//}

bool check_size(string& s , string::size_type sz)
{
	return  s.size() >= sz;
}
int main(int argc, char** argv)
{
	
	vector<string> vs = { "the","quick","red","fox","jumps","over","the","slow","red","turtle" };
	list<string> lis;
	auto iter = partition(vs.begin(), vs.end(), bind(check_size, _1, 5));
	for (const auto s : vs)
		cout << s << " ";
	cout << endl;

	vs.erase(iter, vs.end());
	for (const auto s : vs)
		cout << s << " ";
	cout << endl;
	system("pause");
	return 0;

	
}

10.27

#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
#include<vector>
#include<forward_list>
#include<deque>
#include<algorithm>
#include<list>
#include<functional>
#include<iterator>

using namespace std;

using namespace placeholders;

//bool isShorter(const string& s1, const string& s2) {
//	return s1.size() < s2.size();
//}

vector<string> elimDups(vector<string>& words) {
	sort(words.begin(), words.end());
	auto end_unique = unique(words.begin(), words.end());
	words.erase(end_unique, words.end());
	return words;
}
//
//bool cmp(const string& a)
//{
//	return a.size() >= 4;
//
//}
//
//void biggis(vector<string>& s, vector<string>::size_type sz)
//{
//	elimDups(s);
//	stable_sort(s.begin(), s.end(), [](const string& a, const string& b) {return a.size() < b.size(); });
//	auto it = partition(s.begin(), s.end(), [sz](const string& a, const string& b) {return a.size() < b.size(); });
//
//}
//bool judge_size(string& s, string::size_type sz)
//{
//	return s.size() <= sz;
//}

bool check_size(string& s , string::size_type sz)
{
	return  s.size() >= sz;
}
int main(int argc, char** argv)
{
	
	string a[10] = { "love","love8","love","love","love","h","diuw","diuwudhg257","love","d" };
	vector<string> vec1(a, a + 10);//利用数组初始化vector  
	vector<string> vec2;
	//实现包含头文件iterator
	unique_copy(vec1.cbegin(), vec1.cend(), back_inserter(vec2));//不支持push_front?

	cout << "字符串中的不重复字符为:";
	for (int i = 0; i < vec2.size(); ++i)
	{
		cout << vec2[i] << " ";
	}


	system("pause");
	return 0;

	
}

10.29

#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
#include<vector>
#include<forward_list>
#include<deque>
#include<algorithm>
#include<list>
#include<functional>
#include<iterator>

using namespace std;

using namespace placeholders;


int main(int argc, char** argv)
{
	
	vector<string> str;
	istream_iterator<string> str_read(cin), eof;

	copy(str_read, eof, back_inserter(str));

	for (auto it : str)
	{
		cout << it << " ";
	}
	system("pause");
	return 0;

	
}

10.37

#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
#include<vector>
#include<forward_list>
#include<deque>
#include<algorithm>
#include<list>
#include<functional>
#include<iterator>

using namespace std;

using namespace placeholders;


int main(int argc, char** argv)
{
	/*istream_iterator<int> in_iter(cin), eof;
	ostream_iterator<int> out_iter(cout);*/

	int a[10] = { 1,0,3,0,4,5,8,9,6,5 };
	vector<int> vec1(a, a + 10);
	list<int> list1;
	copy(vec1.rbegin() + 2, vec1.rend() - 3, back_inserter(list1));
	
	
	for (auto it = list1.begin(); it != list1.end(); ++it)

	{
		cout << *it << " ";
	}
	

	return 0;
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值