## STL区间成员函数及区间算法总结

1）更少的函数调用
2）更少的元素移动
3）更少的内存分配

container::container(InputIterator begin, // 区间的起点
InputIterator end); // 区间的终点

int myints[] = { 10, 20, 30, 30, 20, 10, 10, 20 };
std::vector<int> myvector (myints, myints+8);

std::vector<int> second ={10, 20, 30, 30, 20, 10, 10, 20};

std::vector<int> second ({10, 20, 30, 30, 20, 10, 10, 20});

void container::insert(iterator position, // 区间插入的位置
InputIterator begin, // 插入区间的起点
InputIterator end); // 插入区间的终点

int myints[] = { 10, 20, 30, 30, 20, 10, 10, 20 };
std::vector<int> myvector;
myvector.push_back(100);
myvector.insert(myvector.begin(),myints,myints+8); //10 20 30 30 20 10 10 20 100

iterator container::erase(iterator begin, iterator end);

c++98的标准关联容器提供的erase为：

void container::erase(iterator begin, iterator end);

iterator container::erase(const_iterator first, const_iterator last);

void container::assign(InputIterator begin, InputIterator end);

int myints[]={10,20,30,40,50,60,70};
std::vector<int> myvector;
myvector.assign(myints,myints+7);

for_each 区间迭代

for_each:遍历，对每个元素都执行一个动作；
C++98只支持最原始的for循环，很多语言（java、python等）都实现了foreach区间迭代语法，这让C++程序员眼馋了很久；

void myfunction (int& i) {
i += 5;
}
std::vector<int> myvector;
myvector.push_back(10);
myvector.push_back(20);
myvector.push_back(30);
for_each(myvector.begin(),myvector.end(),myfunction); //15 25 35

c++11中新增了区间迭代，使得我们对for_each的依赖降低了，使用也更加方便：

for(auto &i : myvector )
{
i+=5;
}

transform() 区间迭代后新值另存为其它地方

return i+5;
}
void output (int i) {  // output function
std::cout << ' ' << i;
}
std::vector<int> myvector;
myvector.push_back(10);
myvector.push_back(20);
myvector.push_back(30);
std::vector<int> bvector;
bvector.resize(myvector.size());
//输出
for_each(bvector.begin(),bvector.end(),output); //bvector: 15 25 35

copy() 区间复制

int myints[]={10,20,30,40,50,60,70};
std::vector<int> myvector (7);
std::copy ( myints, myints+7, myvector.begin() );

fill() 区间填充

std::vector<int> myvector (8);                       // myvector: 0 0 0 0 0 0 0 0
std::fill (myvector.begin(),myvector.begin()+4,5);   // myvector: 5 5 5 5 0 0 0 0

replace() 区间替换

int myints[] = { 10, 20, 30, 30, 20, 10, 10, 20 };
std::vector<int> myvector (myints, myints+8);            // 10 20 30 30 20 10 10 20
std::replace (myvector.begin(), myvector.end(), 20, 99); // 10 99 30 30 99 10 10 99

bool bigerThen20 (int i) { return i > 20; }
int myints[] = { 10, 20, 30, 30, 20, 10, 10, 20 };
std::vector<int> myvector (myints, myints+8);            // 10 20 30 30 20 10 10 20
std::replace_if (myvector.begin(), myvector.end(), bigerThen20, 99); //10 20 99 99 20 10 10 20

remove() 区间删除

int myints[] = { 10, 20, 30, 30, 20, 10, 10, 20 };
std::vector<int> myvector (myints, myints+8);            // 10 20 30 30 20 10 10 20
std::remove(myvector.begin(), myvector.end(), 20); // 10 30 30 10 10 ? ? ?

myvector.erase(std::remove(myvector.begin(), myvector.end(), 20),myvector.end()); // 10 30 30 10 10

unique() 区间去重

int myints[] = {10,20,20,20,30,30,20,20,10};           // 10 20 20 20 30 30 20 20 10
std::vector<int> myvector (myints,myints+9);
std::vector<int>::iterator it;
it = std::unique (myvector.begin(), myvector.end());   // 10 20 30 20 10 ?  ?  ?  ?
myvector.erase(it,myvector.end());

Win10预览版10122怎么快速建立本地账户？

ajax浏览器兼容的问题探讨
SQL Server出现System.OutOfMemoryException异常的解决方法
javascript中slice(),splice(),split(),substring(),substr()使用方法
mysql压力测试脚本实例
javascript内置对象操作详解

win10中安装的软件不兼容该怎么办？
Macbook Pro/Air合上盖子无法正常睡眠/唤醒的解决方法
js实现目录定位正文示例
Javascript中With语句用法实例