泛型算法之——fill fill_n back_inserter copy replace replace_copy

泛型算法之——fill fill_n back_inserter copy replace  replace_copy(未解决问题)

#include <iostream>
#include <conio.h>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
#include <numeric>
using namespace std;

// 打印容器内容
void printVec(const vector<string> &vecT)
{
 vector<string>::const_iterator vecIt= vecT.begin();
 for (; vecIt != vecT.end(); vecIt ++)
 {
  cout<<*vecIt<<endl;
 }
 cout<<endl;
}
void iniVec( vector<string> &vecT)
{
 vecT.erase(vecT.begin(),vecT.end());
 char ch = 'a';
 string strT = "";
 for (int i=0;i<10;i++)
 {
  vecT.push_back(strT+ch++);
 }
}
void printList(const list<string> &listT)
{
 list<string>::const_iterator listIt= listT.begin();
 for (; listIt != listT.end(); listIt ++)
 {
  cout<<*listIt<<endl;
 }
 cout<<endl;
}
int main()
{
    cout<<"以下是写入容器的算法"<<endl;
 
 vector<string> vecTest;
 iniVec(vecTest);
 printVec(vecTest);
 
 cout<<"fill 安全的写入,严格限定在输入的迭代器范围内,才会写入,当然如果迭代器有问题,也会有未定义行为 "<<endl;
 fill(vecTest.begin()+5, vecTest.end() , "haha" );//用string("haha")也行   vecTest.end()+3这样还是会出错的

 printVec(vecTest);
 iniVec(vecTest);

 cout<<"fill_n 不检查安全写入的算法  没有迭代器范围,只有迭代器起点"<<endl;
 fill_n(vecTest.begin(),3,"fill_n");// 3 改成30超出范围了,就会出现未定义行为

 printVec(vecTest);
 iniVec(vecTest);

 cout<<"back_inserter 插入迭代器 must include iterator head file"<<endl;
 fill_n(back_inserter(vecTest) , 10 , "back_inserter");

 printVec(vecTest);
 iniVec(vecTest);

 cout<<"copy 写入到目标迭代器"<<endl;
 list<string> listTest;
 copy(vecTest.begin(),vecTest.end(), back_inserter(listTest));

 printList(listTest);

 cout<<"等效写法而且效率更高 list<string> listTest2(vecTest.begin(),vecTest.end())"<<endl;
 list<string> listTest2(vecTest.begin(),vecTest.end());
 
 printList(listTest2);

 cout<<"replace 算法的copy版本 "<<endl;
 replace(vecTest.begin(), vecTest.end(), string("c"),string("cccc"));//只能用string("cccc") 因为replace里有const了,与const char ×形成两个const

 printVec(vecTest);
 iniVec(vecTest);

 cout<<"replace_copy 不替换写法"<<endl;
 vector<string> vecTmp;
  replace_copy(vecTest.begin(),vecTest.end() ,back_inserter(vecTmp), string("c"),string("cccc"));//只能用string("cccc")

 printVec(vecTest);
 printVec(vecTmp);

 getch();
 return 0;
}
 

以下是写入容器的算法
a
b
c
d
e
f
g
h
i
j

fill 安全的写入,严格限定在输入的迭代器范围内,才会写入,当然如果迭代器有问题,
也会有未定义行为
a
b
c
d
e
haha
haha
haha
haha
haha

fill_n 不检查安全写入的算法  没有迭代器范围,只有迭代器起点
fill_n
fill_n
fill_n
d
e
f
g
h
i
j

back_inserter 插入迭代器 must include iterator head file
a
b
c
d
e
f
g
h
i
j
back_inserter
back_inserter
back_inserter
back_inserter
back_inserter
back_inserter
back_inserter
back_inserter
back_inserter
back_inserter

copy 写入到目标迭代器
a
b
c
d
e
f
g
h
i
j

等效写法而且效率更高 list<string> listTest2(vecTest.begin(),vecTest.end())
a
b
c
d
e
f
g
h
i
j

replace 算法的copy版本
a
b
cccc
d
e
f
g
h
i
j

replace_copy 不替换写法
a
b
c
d
e
f
g
h
i
j
a
b
cccc
d
e
f
g
h
i
j

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值