c++常用摘抄(一)

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

 

 3.在list 双端链表中的迭代器函数

        std::advance(iterator pos,distance n)    注意:n可以取正或负整数值

 1 // advance example
 2 #include <iostream>     // std::cout
 3 #include <iterator>     // std::advance
 4 #include <list>         // std::list
 5 
 6 int main () {
 7   std::list<int> mylist;
 8   for (int i=0; i<10; i++) mylist.push_back (i*10);
 9 
10   std::list<int>::iterator it = mylist.begin();
11 
12   std::advance (it,5);
13 
14   std::cout << "The sixth element in mylist is: " << *it << '\n';
15     
16   std::advance (it,-4);
17 
18   std::cout << "now let it back_forward: " << *it << '\n';
19   system("PAUSE");
20   return 1;
21 }
22     

 4.std::list::rease和std::list::remove的区别

erase:


Removes from the list container either a single element (position) or a range of elements ([first,last)).

format:
1 iterator erase (iterator position);
2 iterator erase (iterator first, iterator last);//注意:删除的是[first,last)前开后闭区间
 1 // erasing from list
 2 #include <iostream>
 3 #include <list>
 4 
 5 int main ()
 6 {
 7   std::list<int> mylist;
 8   std::list<int>::iterator it1,it2;
 9 
10   // set some values:
11   for (int i=1; i<10; ++i) mylist.push_back(i*10);
12 
13                               // 10 20 30 40 50 60 70 80 90
14   it1 = it2 = mylist.begin(); // ^^
15   advance (it2,6);            // ^                 ^
16   ++it1;                      //    ^              ^
17 
18   it1 = mylist.erase (it1);   // 10 30 40 50 60 70 80 90
19                               //    ^           ^
20 
21   it2 = mylist.erase (it2);   // 10 30 40 50 60 80 90
22                               //    ^           ^
23 
24   ++it1;                      //       ^        ^
25   --it2;                      //       ^     ^
26 
27   mylist.erase (it1,it2);     // 10 30 60 80 90
28                               //        ^
29 
30   std::cout << "mylist contains:";
31   for (it1=mylist.begin(); it1!=mylist.end(); ++it1)
32     std::cout << ' ' << *it1;
33   std::cout << '\n';
34 
35   return 0;
36 }

 remove:

Removes from the container all the elements that compare equal to val

Removes from the container all the elements that compare equal to val
format:
1
void remove (const value_type& val);
 1 // remove from list
 2 #include <iostream>
 3 #include <list>
 4 
 5 int main ()
 6 {
 7   int myints[]= {17,89,7,14};
 8   std::list<int> mylist (myints,myints+4);//good skill:用数组初始化list
 9 
10   mylist.remove(89);
11 
12   std::cout << "mylist contains:";
13   for (std::list<int>::iterator it=mylist.begin(); it!=mylist.end(); ++it)
14     std::cout << ' ' << *it;
15   std::cout << '\n';
16 
17   return 0;
18 }
output:
mylist contains: 17 7 14

 5.list的初始化构造方法

 1 // constructing lists
 2 #include <iostream>
 3 #include <list>
 4 
 5 int main ()
 6 {
 7   // constructors used in the same order as described above:
 8   std::list<int> first;                                // empty list of ints
 9   std::list<int> second (4,100);                       // four ints with value 100
10   std::list<int> third (second.begin(),second.end());  // iterating through second//注意地址的前闭后开
11   std::list<int> fourth (third);                       // a copy of third
12 
13   // the iterator constructor can also be used to construct from arrays:
14   int myints[] = {16,2,77,29};
15   std::list<int> fifth (myints, myints + sizeof(myints) / sizeof(int) );//注意地址的前闭后开
16 17 std::cout << "The contents of fifth are: "; 18 for (std::list<int>::iterator it = fifth.begin(); it != fifth.end(); it++) 19 std::cout << *it << ' '; 20 21 std::cout << '\n'; 22 23 return 0; 24 }

 


posted @ 2015-07-22 16:23  喜欢算法的小杰  阅读(201)  评论(0编辑  收藏  举报