#include
int main() {
int i;
vector vi;
while (cin >> i)
vi.push_back(i);
cout << count(vi.cbegin(), vi.cend(), 6);
}
#include
int main() {
string str;
list lst;
while (cin >> str)
lst.push_back(str);
cout << count(lst.cbegin(), lst.cend(), "a");
}
#include
int main(){
vector vi = {1,2,3,4,5,6,7,8,9,10};
cout << accumulate(vi.cbegin(), vi.cend(), 0) << endl;
}
最终得到的结果将会由于每次将值添加到结果时,因double到int的隐式转换丢失精度。
正常运行并返回正确结果。
int main(){
vector vi(10, 10);
fill_n(vi.begin(), vi.size(), 0);
for (int i : vi)
cout << i << endl;
}
(a)
vector vec; list lst; int i;
while (cin >> i)
lst.push_back(i);
copy(lst.cbegin(), lst.cend(), vec.begin());
(b)
vector vec;
vec.reserve(10);
fill_n(vec.begin(), 10, 0);
(a)有错。vec是空容器,无法向一个空容器去复制数据。
(b)有错。虽然为vec提供了内存空间,但此时vec仍是一个空容器,无法向空容器去写入元素,该语句的结果是未定义的。
对于标准库算法而言,它只会使用赋值算法将容器内的值改变。而当标准库算法使用back_inserter时,赋值运算符将变作调用push_back函数,而非原本的赋值函数。可见这一行为是因为使用了back_inserter才导致改变了容器的大小,对于标准库算法而言仅仅是调用了赋值算法而已,因此不会使这一断言失效。
void eliminate_duplicates(vector &v)
{
sort(v.begin(), v.end());
for (auto i : v)
cout << i << " ";
cout << endl;
auto end_unique = unique(v.begin(), v.end());
for (auto i : v)
cout << i << " ";
cout << endl;
v.erase(end_unique, v.end());
for (auto i : v)
cout << i << " ";
cout << endl;
}
int main()
{
vector v = {"abc", "abc", "efg", "hij", "klmn", "opq", "xyz", "klmn"};
eliminate_duplicates(v);
return 0;
}
因为泛型函数使用的是迭代器,因此其本身就没有容器的概念。这也是为了通用性服务,不考虑容器本身,而通过迭代器来直接访问其中的元素。另外,由于迭代器容易因为元素的删除插入等原因失效,因此为了避免这种情况发生,会限制泛型函数进行增删的操作,避免出现迭代器失效的情况。
void eliminate_duplicates(vector &v)
{
sort(v.begin(), v.end());
auto end_unique = unique(v.begin(), v.end());
v.erase(end_unique, v.end());
}
bool isShorter(const string &s1, const string &s2) {
return s1.size() < s2.size();
}
int main()
{
vector v = { "abc", "abc", "efg", "hij", "klmn", "opq", "xyz", "klmn" };
eliminate_duplicates(v);
stable_sort(v.begin(), v.end(), isShorter);
for (auto i : v)
cout << i << " ";
return 0;
}
void compareIsbn(const Sales_data &S1, const Sales_data &S2){
return S1.isbn() < S2.isbn();
}
bool morethan5(const string &str) {
return str.size() > 4;
}
int main() {
vector v{ "1", "12", "123", "1234", "12345", "123456" };
for (const string &str : v) {
if (morethan5(str))
cout << str << endl;
}
}
int main() {
auto f = [](int i, int j) {return i + j; };
cout << f(1, 2) << endl;
}
int main() {
int i = 5;
auto f = [i](int j) {return i + j; };
cout << f(5);
}
略。
sort(v.begin(), v.end(), [](const Sales_data &s1, const Sales_data &s2)
{return s1.isbn() < s2.isbn(); });
void eliminate_duplicates(vector &v)
{
sort(v.begin(), v.end());
auto end_unique = unique(v.begin(), v.end());
v.erase(end_unique, v.end());
}
void biggies(vector &words, vector::size_type sz) {
eliminate_duplicates(words);
stable_sort(words.begin(), words.end(), [](const string &a, const string &b) {return a.size() < b.size(); });
auto wz = partition(words.begin(), words.end(), [sz](const string &a) {return a.size() < sz; });
auto count = words.end() - wz;
for_each(wz, words.end(), [](const string &s) { cout << s << " "; });
}
int main() {
std::vector v = { "a", "b", "bc", "abc", "1234", "12345", "123456" };
biggies(v, 4);
}
void eliminate_duplicates(vector &v)
{
sort(v.begin(), v.end());
auto end_unique = unique(v.begin(), v.end());
v.erase(end_unique, v.end());
}
void biggies(vector &words, vector::size_type sz) {
eliminate_duplicates(words);
stable_sort(words.begin(), words.end(), [](const string &a, const string &b) {return a.size() < b.size(); });
auto wz = stable_partition(words.begin(), words.end(), [sz](const string &a) {return a.size() >= sz; });
auto count = words.end() - wz;
for_each(wz, words.end(), [](const string &s) { cout << s << " "; });
}
int main() {
std::vector v = { "13", "12", "123", "1234", "12345", "123456" };
biggies(v, 4);
}
void eliminate_duplicates(vector &v)
{
sort(v.begin(), v.end());
auto end_unique = unique(v.begin(), v.end());
v.erase(end_unique, v.end());
}
int biggies(vector &words, vector::size_type sz) {
eliminate_duplicates(words);
auto count = count_if(words.begin(), words.end(), [sz](const string &a) {return a.size() >= sz; });
return count;
}
int main() {
std::vector v = { "13", "12", "123", "1234", "12345", "123456" };
cout << biggies(v, 4);
}
int main() {
int i = 10;
auto f = [&i]() {if (i == 0) return true; else { while (i != 0) --i; return false; }};
cout << f() << " " << i << endl;
cout << f() << " " << i << endl;
}
void eliminate_duplicates(vector &v)
{
sort(v.begin(), v.end());
auto end_unique = unique(v.begin(), v.end());
v.erase(end_unique, v.end());
}
bool check_size(const string &str, string::size_type sz) {
return str.size() >= sz;
}
int biggies(vector &words, vector::size_type sz) {
eliminate_duplicates(words);
auto count = count_if(words.begin(), words.end(), bind(check_size, _1, sz));
return count;
}
int main() {
std::vector v = { "13", "12", "123", "1234", "12345", "123456" };
cout << biggies(v, 4);
}
bind函数接收一个函数,并接收一个参数列表,其中参数列表的数量没有限制。
bool check_size(const string &str, string::size_type sz) {
return str.size() < sz;
}
int main(){
string str = "hello";
vector vi = {1,2,3,4,5,6,7,8,9};
find_if(vi.begin(), vi.end(), bind(check_size, str, _1));
}
void eliminate_duplicates(vector &v)
{
sort(v.begin(), v.end());
auto end_unique = unique(v.begin(), v.end());
v.erase(end_unique, v.end());
}
bool check_size(const string &str, string::size_type sz) {
return str.size() < sz;
}
void biggies(vector &words, vector::size_type sz) {
eliminate_duplicates(words);
stable_sort(words.begin(), words.end(), [](const string &a, const string &b) {return a.size() < b.size(); });
auto wz = partition(words.begin(), words.end(), bind(check_size, _1, sz));
auto count = words.end() - wz;
for_each(wz, words.end(), [](const string &s) { cout << s << " "; });
}
int main() {
std::vector v = { "a", "b", "bc", "abc", "1234", "12345", "123456" };
biggies(v, 4);
}
back_inserter 创建一个使用push_back的迭代器,将元素插入的容器末尾。接收被插入元素一个参数。
front_inserter 创建一个使用push_front的迭代器,将元素插入到容器头部。接收被插入元素一个参数。
inserter 创建一个使用insert的迭代器,将元素插入到指定位置。接收被插入元素和目标位置两个参数。
int main() {
vector v1 = { 1,1,2,2,3,3,4,4,5,5 };
vector v2;
unique_copy(v1.begin(), v1.end(), back_inserter(v2));
for (int i : v2)
cout << i << endl;
}
int main() {
vector v1 = { 1,2,3,4,5,6,7,8,9 };
list lst1;
list lst2;
list lst3;
copy(v1.begin(), v1.end(), inserter(lst1, lst1.begin()));
for (int i : lst1) //{1,2,3,4,5,6,7,8,9}
cout << i;
cout << endl;
copy(v1.begin(), v1.end(), back_inserter(lst2));
for (int i : lst2) //{1,2,3,4,5,6,7,8,9}
cout << i;
cout << endl;
copy(v1.begin(), v1.end(), front_inserter(lst3));
for (int i : lst3) //{9,8,7,6,5,4,3,2,1}
cout << i;
}
int main(){
string str = "file_name";
ifstream input(stf);
istream_iterator iter(input), eof;
vector v(iter(input), eof);
ostream_iterator iter_out(cout, "\n");
copy(v.begin(), v.end(), iter_out);
cout << endl;
}
int main() {
istream_iterator in(cin), eof;
vector v(in, eof);
sort(v.begin(), v.end());
ostream_iterator out(cout, "\n");
copy(v.begin(), v.end(), out);
cout << endl;
}
int main() {
istream_iterator in(cin), eof;
vector v(in, eof);
sort(v.begin(), v.end());
ostream_iterator out(cout, "\n");
unique_copy(v.begin(), v.end(), out);
cout << endl;
}
void func(const string& input_filename, const string& output_filename1, const string& output_filename2){
ifstream in(input_filename);
istream_iterator iter_in(in), eof;
ostream_iterator iter_out1(ofstream(output_filename1), " "), ite_out2(ofsteam(out_filename2), "\n");
while(iter_in != eof){
if (i&1 == 1)
*iter_out1++ = i;
else
*iter_out2++ = i;
++iter_in;
}
}
int main() {
vector v = { 1,2,3,4,5,6,7,8 };
for (auto riter = v.crbegin(); riter != v.crend(); ++riter)
cout << *riter << endl;
}
int main() {
vector v = { 1,2,3,4,5,6,7,8 };
for (auto iter = v.cend() - 1; iter >= v.cbegin(); --iter) {
cout << *iter << endl;
if (iter == v.cbegin())
break;
}
}
int main() {
list lst = { 1,0,1,0,1,0,1 };
auto index = find(lst.crbegin(), lst.crend(), 0);
cout << "last zero: " << *index << endl;
}
int main() {
vector vi = { 0,1,2,3,4,5,6,7,8,9 };
list lst;
copy(vi.crbegin() + 2, vi.crend() - 3, back_inserter(lst));
for (int i : lst)
cout << i << endl;
}
输入迭代器(input iterator): iter1==iter2 、iter1!=iter2 、 iter++、 *iter 、 iter->xx
输出迭代器(output iterator): ++iter、 *iter
前向迭代器(forward iterator): iter1==iter2 、iter1!=iter2 、 iter++、 *iter 、 iter->xx
双向迭代器(bidirectional iterator): iter1==iter2 、iter1!=iter2 、 iter++、 iter-- 、*iter 、 iter->xx
随机访问迭代器(random-access iterator):iter1==iter2 、iter1!=iter2、iter++、iter-- 、*iter 、 iter->xx 、 iter1(< 、 <= 、 > 、 >=)iter2、 iter(+、+=、-、-=)i 、iter1-iter2、iter[n]
list:前向迭代器。vector:随机访问迭代器。
copy:前两个为输入,后一个为输出。
reverse:双向迭代器。
unique:前向迭代器。
replace(beg, end, old_val, new_val);
将beg
至end
范围内所有值为old_val
的元素替换为new_val
。
replace_if(beg, end, pred, new_val);
将beg
至end
范围内所有通过谓词pred
判定为真的元素替换为new_val
。
replace_copy(beg, end, dest, old_val, new_val);
将beg
至end
范围内所有值为old_val
的元素替换为new_val
的结果复制到dest迭代器及其后续部分。不改变原容器内的值。
replace_copy_if(beg, end, dest, pred, new_val);
将beg
至end
范围内所有通过谓词pred
判定为真的元素替换为new_val
的结果复制到dest迭代器及其后续部分。不改变原容器内的值。
void elimDups(list &words){
sort(words.begin(), words.end());
list.unique();
}