c++ STL ѧϰ±Ê¼Ç
for Ñ»·µÄËÄÖÖÓ÷¨
int nArray[] = {0, 1, 2, 3, 4, 5};
vector<int> vec(nArray, nArray + 6);
-
ÓÃϱê for (int i = 0; i < vec.size(); ++i)
cout << vec[i] << " ";
-
Óõü´úÆ÷ for (auto it = vec.begin(); it != vec.end(); ++it)
cout << (*it) << " ";
for (vector<int>::iterator it = vec.begin(); it != vec.end(); ++it)
cout << (*it) << " ";
-
STLº¯Êý
for_each(vec.begin(), vec.end(), [](int item) { cout << item << " "; });
-
lambda ±í´ïʽ
-
»ù±¾Óï·¨
[] { cout << "hello lambda1" << endl; }();
auto l = [] { cout << "hello lambda2" << endl; };
l();
-
Lambda ¿ÉÒÔ·µ»ØijÎï¡£µ«²»ÐèÒªÖ¸Ã÷·µ»ØÀàÐÍ int a = [] { return 42; }();
-
·½À¨ºÅÄÚ,¿ÉÒÔÖ¸Ã÷Ò»¸öcaptureÓÃÀ´´¦ÀíÍⲿ×÷ÓÃÓòÄÚδ±»´«µÝΪʵ²ÎµÄÊý¾Ý int x = 1, y = 42;
auto q = [x, &y] {
y++;
cout << "x: " << x << endl;
cout << "y: " << y << endl;
};
x = y = 77;
q();
int x = 1, y = 42;
x = y = 77;
auto q = [x, &y] {
y++;
cout << "x: " << x << endl;
cout << "y: " << y << endl;
};
q();
-
ÐÂÔöÌØÐÔ for (int item : vec)
cout << item << " ";
³£Óú¯Êý(#include <Algorithom>)
-
sort(start, end, cmp)
-
lower_bound(start, end, val, cmp)
-
µÚËĸö²ÎÊýÊDZȽϷ½·¨,¿ÉÒÔÊ¡ÂÔ -
×¢Òâ:ʹÓÃlower_bound()±ØÐëÌáÇ°ÅÅÐò¡£ -
ÔÚ [start, end) ÇøÓòÄÚ²éÕÒ²»Ð¡ÓÚ(>=) value µÄÔªËØ int a[6] = {1, 3, 5, 7, 9, 11};
int* i = lower_bound(a, a + 6, 10);
cout << "ÊýֵΪ:" << (*i);
cout << "ϱíΪ:" << i - a;
-
ÔÚ [start, end) ÇøÓòÄÚ²éÕÒµÚÒ»¸ö²»·ûºÏ cmp ¹æÔòµÄÔªËØ int a[6] = {1, 3, 5, 7, 9, 11};
int* i = lower_bound(a, a + 6, 6, [](int x, int y) { return x <= y; });
cout << "ÊýֵΪ:" << (*i);
cout << "ϱíΪ:" << i - a;
-
·µ»ØÖµ:Èç¹ûÕÒµ½·µ»ØÕÒµ½ÔªËصĵØÖ··ñÔò·µ»ØendµÄµØÖ·¡£(ÕâÀï×¢ÒâÓпÉÄÜÔ½½ç) int a[6] = {1, 3, 5, 7, 9, 11};
int* i = lower_bound(a, a + 6, 12);
cout << "ÊýֵΪ:" << (*i);
-
upper_bound(start, end, val, cmp)
-
Óëlower_bound()ͬÀí,¿ÉÒÔÀí½âΪ;upper_bound()ÊÇ>,¶ølower_boundÊÇ>= int a[6] = {1, 3, 5, 7, 9, 11};
int* i = upper_bound(a, a + 6, 7);
cout << "ÊýֵΪ:" << (*i) << endl;
cout << "ϱíΪ:" << i - a;
-
next_permutation(start, end, cmp)
-
µÚËĸö²ÎÊýÊDZȽϷ½·¨,¿ÉÒÔÊ¡ÂÔ -
ÇóÒ»¸öÅÅÐòµÄºóÃæÅÅÁеĺ¯Êý int a[3] = {1, 3, 2};
do {
for (int x : a) {
cout << x << " ";
}
cout << endl;
} while (next_permutation(a, a + 3));
-
cmpÖ¸¶¨ÅÅÐò·½·¨ bool Compare(int x, int y) {
if (x > y)
return true;
else
return false;
}
int a[3] = {1, 3, 2};
do {
for (int x : a) {
cout << x << " ";
}
cout << endl;
} while (next_permutation(a, a + 3,Compare));
-
prev_permutation(start, end ,cmp)
- ºÍ next_permutation º¯ÊýÒ»Ñù,Ö»ÊÇÇóÒ»¸öÅÅÐòµÄÇ°ÃæÅÅÁеĺ¯Êý
-
unique(start, end)
-
ʹÓøú¯ÊýÇ°,Ò»¶¨ÒªÏȶÔÐòÁнøÐÐÅÅÐò -
unique()½«²»Öظ´µÄÔªËطŵ½ÈÝÆ÷µÄÇ°Ãæ,·µ»ØÖµÊÇÈ¥ÖØÖ®ºóµÄβµØÖ·¡£ int a[4] = {1, 2, 3, 3};
int k = unique(a, a + 4)-a;
for (int i = 0; i < k;i++){
cout << a[i];
}
³£ÓÃÈÝÆ÷
-
string ³£Ó÷½·¨:
-
size()¡¢length()¡ª¡ª·µ»Ø×Ö·û´®³¤¶È -
empty()¡ª¡ªÅжÏ×Ö·û´®ÊÇ·ñΪ¿Õ -
clear()¡ª¡ªÇå¿Õ×Ö·û´® -
substr(start, size)¡ª¡ª·µ»Ø×Ó´®
- start ÊÇÒª»ñÈ¡µÄ×Ó´®µÄÆðʼµØÖ·,sizeÊÇÒª»ñÈ¡µÄ³¤¶È
string a = "abcd";
string b = a.substr(1, 2);
cout << b;
-
c_str()¡ª¡ª·µ»Ø×Ö·û´®ËùÔÚ×Ö·ûÊý×éµÄÆðʼµØÖ· string a = "abcd";
const char* b = a.c_str();
cout << b;
-
queue<T>(ÏȽøÏȳö) ³£Ó÷½·¨:
-
priority_queue<T>(ÓÅÏȶÓÁÐ) ¶¨Òå:priority_queue<Type, Container, Functional>
-
TypeÊÇ´æ´¢Êý¾ÝµÄÀàÐÍ -
ContainerÊÇ´æ´¢Êý¾ÝµÄÈÝÆ÷µÄÀàÐÍ(ĬÈÏΪvctor) -
FunctionalÊDZȽϵķ½·¨(ÒªÓּ¯ÊýµÄÐÎʽʵÏÖ,ĬÈÏΪless(ÉýÐòÅÅÁÐ))
- ·Âº¯Êý²»ÊǺ¯Êý,ËüÊÇÒ»¸öÀà
- ·Âº¯ÊýÖØÔØÁË()ÔËËã·û,ʹµÃËüµÄ¶ÔÄã¿ÉÒÔÏñº¯ÊýÄÇÑù×Óµ÷ÓÃ
class Cmp {
public:
bool operator()(int x, int y) { return x > y; }
};
Cmp cmp;
cout << cmp(1, 2);
³£Ó÷½·¨:
-
stack(ÏȽøºó³ö) ³£Ó÷½·¨:
- size()¡ª¡ª·µ»Ø³¤¶È
- empty()¡ª¡ªÅпÕ
- push()¡ª¡ªÌí¼ÓÔªËØ
- pop()¡ª¡ªµ¯³öÕ»¶¥ÔªËØ,ÎÞ·µ»ØÖµ
- top()¡ª¡ª·µ»ØÕ»¶¥ÔªËØ
-
map
-
ʹÓÃ[]½øÐвåÈë map<char, int> a;
a['a'] = 1;
-
insert(pos, value) map<char, int> a;
a.insert({'a', 1});
-
at(key)¡ª¡ª·µ»ØkeyµÄvalue,Èç¹ûûÓеĻ°±¨´í map<char, int> a;
a.insert({'a', 1});
cout << a.at('a');
-
eraze(key)¡ª¡ªÉ¾³ýÖ¸¶¨¼üÖµ¶Ô,ɾ³ý³É¹¦·µ»Ø1,·ñÔò0 map<char, int> a;
a.insert({'a', 1});
cout << a.erase('a') << endl;
cout << a['a'] << endl;
-
unordered_map
-
vector Ö§³ÖÊý×éÐÎʽֱ½Ó·ÃÎÊ ³£Óú¯Êý:
- size()
- empty()
- clear()
- front()
- back()
- push_back()
- pop_back()
- begin()
- end()
|