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;
}
|