标签:
1.标准初始化函数
std::fill(首地址,尾地址,value) || 用于在首尾地址之间填充value值,对应matlab的ones(1:n)函数
1 template <class ForwardIterator, class T> 2 void fill (ForwardIterator first, ForwardIterator last, const T& val) 3 { 4 while (first != last) { 5 *first = val; 6 ++first; 7 } 8 }
1 // fill algorithm example 2 #include <iostream> // std::cout 3 #include <algorithm> // std::fill 4 #include <vector> // std::vector 5 6 int main () { 7 std::vector<int> myvector (8); // myvector: 0 0 0 0 0 0 0 0 8 9 std::fill (myvector.begin(),myvector.begin()+4,5); // myvector: 5 5 5 5 0 0 0 0 10 std::fill (myvector.begin()+3,myvector.end()-2,8); // myvector: 5 5 5 8 8 8 0 0 11 12 std::cout << "myvector contains:"; 13 for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) 14 std::cout << ‘ ‘ << *it; 15 std::cout << ‘\n‘; 16 17 return 0; 18 }
myvector contains: 5 5 5 8 8 8 0 0
2.交换函数
std::swap(value1,value2)
1 template <class T> void swap ( T& a, T& b ) 2 { 3 T c(a); a=b; b=c; 4 }
1 // swap algorithm example (C++11) 2 #include <iostream> // std::cout 3 #include <utility> // std::swap 4 5 int main () { 6 7 int x=10, y=20; // x:10 y:20 8 std::swap(x,y); // x:20 y:10 9 10 int foo[4]; // foo: ? ? ? ? 11 int bar[] = {10,20,30,40}; // foo: ? ? ? ? bar: 10 20 30 40 12 std::swap(foo,bar); // foo: 10 20 30 40 bar: ? ? ? ? 13 14 std::cout << "foo contains:"; 15 for (int i: foo) std::cout << ‘ ‘ << i; 16 std::cout << ‘\n‘; 17 18 return 0; 19 }
foo contains: 10 20 30 40
标签:
原文地址:http://www.cnblogs.com/jieforever/p/4667810.html