有C语言基础,通过短时间学习stl达到方便刷算法题的水平。
文中可能有些许错误,欢迎指出。
略
在< algorithm >头文件中。
#include<iostream>
#include<algorithm>
using namespace std;
int main() {
int a[] = { 5,3,12,-33,-5,6,99 };
sort(a, a + 7);
for (int i = 0; i < 7; i++) {
cout << a[i] << " ";
}
cout << endl;
return 0;
}
string ss = "abcdefgggggg";
cout << ss << endl; //abcdefgggggg
cout << ss[1]<<endl;//b
使用cin,读取到空格就会停止读取。
使用< string >头文件中的getline函数,可以读取空格,直接获取一行数据。
string ss;
getline(cin, ss);//hello world
cout << ss;//hello world
+=运算符对字符串和字符也有效
string ss;
ss += "hello";
ss += " world";
ss += '5';
ss += 97;//a的ascii码为97
int b = 4;
ss += (b + '0');//0字符的ascii码向右移动5位
cout << ss;
输出结果为:hello world5a4
在< algorithm >头文件中。
string ss = "55412138";
sort(ss.begin(), ss.end());
cout << ss;
输出结果为:11234558
ss.begin()是字符串头部的指针,ss.end()是尾部的指针。
string ss = "56421973";
ss.erase(ss.begin()); //6421973删除第一个
ss.erase(--ss.end()); //642197删除最后一个
cout << ss << endl;
string ss = "56421973";
ss = ss.substr(2, 4);//从ss[2]取4位,即为4219
ss = ss.substr(1, -1);//从ss[1]取到最后,即为219
cout << ss;
1.for循环
string ss = "56421973";
for (int i = 0; i < ss.length(); i++)
cout<<ss[i];
2.迭代器
string ss = "56421973";
for (string::iterator it = ss.begin(); it != ss.end(); it++)
cout << *it;
3.迭代器化简
string ss = "56421973";
for (auto it = ss.begin(); it != ss.end(); it++)
cout << *it;
4.利用C++ 11新特性for循环
string ss = "56421973";
for (auto x:ss)
cout << x;
vector<int> v1;//定义一个空vector,初始为0
vector<int> v2(5);//定义一个大小为5的vector,初始为0
vector<int> v3(5, 6);//定义一个大小为6的vector,初始为6
vector<int> v4{ 1,2,3,4,5 };//定义的vector中数字为1,2,3,4,5
for (auto x : v3)
cout << x;
vector<int> v{ 1,2,3,4,5 };
cout << v[2]<<endl;
cout << v.at(2);
输出结果为: 3\n3
vector<int> v{ 1,2,3,4,5 };
v.push_back(6);
v.push_back(7);
for (auto x : v)
cout << x;
输出结果: 1234567
vector<int> v{ 1,2,3,4,5 };
v.resize(10);
for (auto x : v)
cout << x;
输出结果:1234500000
v.erase(v.begin());//删除第一个元素
v.erase(--v.end());//删除最后一个元素
cout << v.front();
cout << v[0];
cout << *v.begin();
cout << v.back();
cout << v[v.size() - 1];
cout << *--v.end();
vector<int> v{ 5,1,2,4,3,-6,8 };
sort(v.begin(), v.end(), less<int>());//从小到大排序
sort(v.begin(), v.end(), greater<int>());//从大到小排序
for (auto x : v) cout << x;
vector<int> v{ 1,2,3,4,5 };
for (int i = 0; i < v.size(); i++) cout << v[i];
cout << endl;
for (vector<int>::iterator it = v.begin(); it != v.end(); it++) cout << *it;//迭代器循环
cout << endl;
for (auto it = v.begin(); it != v.end(); it++) cout << *it;//迭代器简化循环
cout << endl;
for (auto x : v) cout << x;//c++11新特性
stack<int> s;
stack<int> s;
s.push(2);
s.push(3);
cout<<s.top();
cout << s.size();
s.pop();
int itob(int decimal) {
stack<int> s;
int res = 0;
while (decimal != 0) {
s.push(decimal % 2);
decimal /= 2;
}
while (!s.empty()) {
res = res * 10 + s.top();
s.pop();
}
return res;
}
输入一行字符串,将字符串逆序打印
输入:hello world my name is steve yu
输出:yu steve is name my world hello
#include<iostream>
#include<stack>
#include<sstream>
using namespace std;
int main() {
string str;
stack<string> s;
getline(cin, str);
stringstream ss;
ss << str;
while (ss >> str)
s.push(str);
while (!s.empty()) {
cout << s.top();
s.pop();
if (s.size() != 0)
cout << " ";
}
return 0;
}
方法1
string s = "1234";
int i;
stringstream ss;
ss << s;
ss >> i;
cout << i;
方法2
string s = "1234";
int i = stoi(s);
cout << i;
方法1
int a = 1234;
string out;
stringstream ss;
ss << a;
ss >> out;
cout << out << endl;
方法2(c++11新特性)
int a = 1234;
cout << to_string(a) << endl;
queue<int> q;
queue<int> q;
q.push(5);
q.push(6);
cout << q.front() << endl;
q.pop();
cout << q.front() << endl;
cout << q.size() << endl;
map:树状表 unordered_map:哈希表
map<int, int> m;//有序的,树状结构(底层)
m[6] = 3;
m[7] = 8;
m[4] = 9;
for (auto it = m.begin(); it != m.end(); it++)
cout << it->first << " " << it->second << endl;
for (auto tmp : m)
cout << tmp.first << " " << tmp.second << endl;
按照左边从小到大排序输出:4 9
6 3
7 8
unordered_map<int, int> m;//无序的,哈希结构(底层)
m[6] = 3;
m[7] = 8;
m[4] = 9;
for (auto it = m.begin(); it != m.end(); it++)
cout << it->first << " " << it->second << endl;
for (auto tmp : m)
cout << tmp.first << " " << tmp.second << endl;
无序输出:6 3
7 8
4 9
bool cmp(pair<int, int> a, pair<int, int> b) {
return a.first > b.first;
}
int main() {
unordered_map<int, int> m;//无序的,哈希结构(底层)
m[6] = 3;
m[7] = 8;
m[4] = 9;
vector<pair<int, int>> v(m.begin(), m.end());
sort(v.begin(), v.end(), cmp);
for (auto tmp : v) {
cout << tmp.first << tmp.second << endl;
}
return 0;
}
按照左边从大到小排序: 78
63
49
集合
s.insert() 添加元素,元素不能重复
s.size() 集合大小
set<int> s;//树状结构,其元素按照从小到大排序
unordered_set<int> s2;//哈希结构,无序,快
s.insert(4);
s.insert(1);
s.insert(2);
s.insert(6);
s.insert(6);
cout << s.size() << endl;
for (auto tmp : s)
cout << tmp << " ";
cout << endl;
for (auto it = s.begin(); it != s.end(); it++)
cout << *it << " ";
cout << endl;
输出结果: 4
1 2 4 6
1 2 4 6
deque<int> d;
//4 9 1 2
d.push_back(1);
d.push_back(2);
d.push_front(9);
d.push_front(4);
d.pop_back();
d.pop_front();
for (auto tmp : d) cout << tmp << " ";
cout << endl;
for (auto it = d.begin(); it != d.end(); it++) cout << *it <<" ";
排序
sort(d.begin(), d.end(), greater<int>());
push_back() 和 emplace_back() 相同
list<int> li;
li.push_back(6);
li.push_front(5);
li.emplace_front(9);
li.emplace_back(10);
li.insert(++li.begin(), 2);
for (auto tmp : li) cout << tmp << " ";
cout << endl;
for (auto it = li.begin(); it != li.end(); it++) cout << *it << " ";
输出:9 2 5 6 10
英文http://www.cplusplus.com/reference/stl/
C语言中文网http://c.biancheng.net/stl/map/
参考文献:https://www.cnblogs.com/littlepage/p/12113748.html
原文:https://www.cnblogs.com/aabyss/p/12284034.html