stl 总结

http://www.cplusplus.com/reference/

超级好的参考资料


vector

//清空vector

vector.clear();

myvector.push_back(v);

myvector.pop_back();

//initial.
int myints[] = {32,71,12,45,26,80,53,33};
std::vector<int> myvector (myints, myints+8);  
//Output.
for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
  std::cout << ' ' << *it;
std::cout << '\n';

Element access:

Iterators:


--------------------------------------

string

  std::string s0 ("Initial string");

  // constructors used in the same order as described above:
  std::string s1;
  std::string s2 (s0);
  std::string s3 (s0, 8, 3);
  std::string s4 ("A character sequence", 6);
  std::string s5 ("Another character sequence");
  std::string s6 (10, 'x');
  std::string s7a (10, 42);      // 42 is the ASCII code for '*'
  std::string s7b (s0.begin(), s0.begin()+7);

std::string str="We think in generalities, but we live in details.";
                                           // (quoting Alfred N. Whitehead)

  std::string str2 = str.substr (12,12);   // "generalities"

  std::size_t pos = str.find("live");      // position of "live" in str

  std::string str3 = str.substr (pos);     // get from "live" to the end

s.substr(begin_index,numbers);

s.find(c)!=string::npos //find it


Convert from strings
Convert to strings
Range access
Iterators:

Capacity:

Element access:

Modifiers:

String operations:

Member constants


------------------------------------------

stack

mystack.pop();

mystack.top();

mystack.push_back();

-----------------------------------------

queue


---------------------------------------

unordered_map

#include <tr1/unordered_map>
using namespace std;

using namespace std::tr1;


unordered_map<int,int> mapping;

mapping.find(gap)!=mapping.end()

Element access
Element lookup
Modifiers

----------------------------------------

unordered_set


-----------------------------------------

pair

make_pair(a,b);

pair<int,int> mypair[2]={{1,2},{3,3}} ;

pair<int,int> mypair={1,2};



-------------------------------------------

ListNode

struct ListNode{

int val;

ListNode *next;

ListNode(int x) : val(x),next(nullptr){}

};


--------------------------------------------

TreeNode

struct TreeNode{

int val;

TreeNode *left;

TreeNode *right;

TreeNode(int x) : val(x),left(nullptr),right(nullptr){}

};


----------------------------------------

UndirectedGraphNode

struct UndirectedGraphNode{

int label;

vector<UndirectedGraphNode*> neighbors;

UndirectedGraphNode(int x) : label(x) {}

};


-----------------------------------------

transform(source_begin,source_end, target_begin, function_pointer);

::tolower

::isalnum;

::atoi;

::isspace;

::isdigit;

::strtod;

::atol;

::strtol;

::strtoul;


--------------------------------------------

#include <algorithm>

void replace (ForwardIterator first, ForwardIterator last,
                const T& old_value, const T& new_value)

void fill (ForwardIterator first, ForwardIterator last, const T& val)
OutputIterator fill_n (OutputIterator first, Size n, const T& val)

void swap ( T& a, T& b )
OutputIterator copy (InputIterator first, InputIterator last, OutputIterator result)
E.G.
--int myints[]={10,20,30,40,50,60,70};
  std::vector<int> myvector (7);
  std::copy ( myints, myints+7, myvector.begin() );
InputIterator find (InputIterator first, InputIterator last, 
InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred)

const T& valFunction for_each(InputIterator first, InputIterator last, Function fn)
E.G.
void myfunction (int i) { std::cout << ' ' << i;}
--for_each (myvector.begin(), myvector.end(), myfunction);
OutputIterator transform (InputIterator first1, InputIterator last1,
                            OutputIterator result, UnaryOperator op)
std::sort (myvector.begin()+4, myvector.end(), myfunction);
bool myfunction (int i,int j) { return (i<j); }
ForwardIterator1 search ( ForwardIterator1 first1, ForwardIterator1 last1,
                            ForwardIterator2 first2, ForwardIterator2 last2)
bool equal ( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2 )
void generate ( ForwardIterator first, ForwardIterator last, Generator gen )
E.G.
--int RandomNumber () { return (std::rand()%100); }
std::generate (myvector.begin(), myvector.end(), RandomNumber);
void generate_n ( OutputIterator first, Size n, Generator gen )
ForwardIterator remove (ForwardIterator first, ForwardIterator last, const T& val)
//heap
int main () {
  int myints[] = {10,20,30,5,15};
  std::vector<int> v(myints,myints+5);

  std::make_heap (v.begin(),v.end());
  std::cout << "initial max heap   : " << v.front() << '\n';

  std::pop_heap (v.begin(),v.end()); v.pop_back();
  std::cout << "max heap after pop : " << v.front() << '\n';

  v.push_back(99); std::push_heap (v.begin(),v.end());
  std::cout << "max heap after push: " << v.front() << '\n';

  std::sort_heap (v.begin(),v.end());

  std::cout << "final sorted range :";
  for (unsigned i=0; i<v.size(); i++)
    std::cout << ' ' << v[i];

  std::cout << '\n';

  return 0;
}
bool all_of (InputIterator first, InputIterator last, UnaryPredicate pred)
E.G.
--if ( std::all_of(foo.begin(), foo.end(), [](int i){return i%2;}) )
    std::cout << "All the elements are odd numbers.\n";
bool any_of (InputIterator first, InputIterator last, UnaryPredicate pred)
bool none_of (InputIterator first, InputIterator last, UnaryPredicate pred)
std::random_shuffle (myvector.begin(), myvector.end());
std::nth_element (myvector.begin(), myvector.begin()+5, myvector.end());
bool binary_search (ForwardIterator first, ForwardIterator last, const T& val)
BidirectionalIterator partition (BidirectionalIterator first,
                                   BidirectionalIterator last, UnaryPredicate pred)
next_permutation(iter_1,iter_2)


------------------------------------------------------

//No need

//#include <iteration>

distance (InputIterator first, InputIterator last);
auto begin (Container& cont)
ForwardIterator next (ForwardIterator it,
       typename iterator_traits<ForwardIterator>::difference_type n = 1)
BidirectionalIterator prev (BidirectionalIterator it,
       typename iterator_traits<BidirectionalIterator>::difference_type n = 1);
void advance (InputIterator& it, Distance n);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值