常用算法操作

2018-10-16  本文已影响13人  downdemo
count(s.begin(), s.end(), x);
string s("hello world");
string s2 = s.substr(0, 5); // 从位置0开始的5个字符的拷贝,hello
string s3 = s.substr(6); // world
size_t pos = str.find("world"); // pos = 6
string s7 = str.substr(pos); // world
string s("123456");
auto pos1 = s.find("2");
auto pos2 = s.find("5");
s.erase(pos1, pos2 - pos1);
cout << s; // 156
string s("do.wn.de.mo@qq.com");
auto pos = s.find("@");
s.erase(remove(s.begin(), s.begin() + pos, '.'), s.begin() + pos);
cout << s; // downdemo@qq.com
auto it = v.begin();
while(it != v.end())
{
    if (*it == 42) it = v.erase(it);
    else ++it;
}
for(auto& x : s) x = tolower(x);
s.insert(tmp);
s.insert(v.begin(), v.end());

m.insert(make_pair(key, value));
sort(v.begin(), v.end(), [](int i, int j){ return i%2 < j%2;});
while(n)
{
    if(n & 1) ++count;
    n >>= 1; // 注意不是n>>1
}
vector['y' - 'a']; // 差值计算索引

vector<int> v(100, 0);
for (auto x : str) v[x]++; // 字符串中每个字母出现次数
reverse(v.begin(), v.end());
list<int> v;
for(int i = 0; i < 10; ++i) v.push_back(i); // push_front插入到首元素
v.pop_back(); // 移除尾元素
v.pop_front(); // 移除首元素 
v.back(); // 尾元素
v.front(); // 首元素
上一篇 下一篇

猜你喜欢

热点阅读