目录
#include <iostream>
#include<algorithm>
#include<queue>
#include<set>
using namespace std;
int main()
{
string str = "abcde";
cout << "翻转前:" << str << endl;
reverse(str.begin(), str.end());//字符串翻转
cout << "翻转后:" << str << endl << endl;
str = "abcde";
cout << "当前字符串:" << str<<endl;
int result = str.find("de");//字串的查找
cout << "找到了返回首字符位置:"<<result<< endl;
result = str.find("aa");
cout << "找不到则返回:" << result << endl << endl;
str.replace(2, 2, "zdy");//字符串的替换
cout <<"替换后的字符串:"<< str << endl;
cout <<"字符串截取的方式一(截取到末尾):" << str.substr(2, 3) << endl;
cout << "字符串截取的方式二(按区间截取):" << str.substr(2) << endl << endl;
cout << "字符串排序前:" << str << endl;
sort(str.begin(), str.end());//sort对字符串排序
cout << "字符串排序后:" << str << endl;
return 0;
}
?
#include <iostream>
#include<algorithm>
#include<set>
using namespace std;
int main()
{
set<int> st;
st.insert(3);
st.insert(5);
st.insert(2);
st.insert(3);
st.insert(4);//自动排序
cout << "set容器的大小:" << st.size() << endl << endl;
//查找某个值,如果找到了则返回指向该元素的迭代器;
//时间复杂度为O(logN)
//如果没有找到则返回指向容器末尾的迭代器,即st.end().
//这是一个表示无效位置的迭代器,指示元素不存在于集合中。
set<int>::iterator ite = st.find(6);
if (ite == st.end()) cout << "没有找到" << endl;
else cout << "找到了,值为:" << *ite << endl;
ite = st.find(2);
if (ite == st.end()) cout << "没有找到" << endl;
else cout << "找到了,值为:" << *ite << endl;
cout << endl;
cout << "迭代器的方式遍历结果为:";
for (ite = st.begin(); ite != st.end(); ite++)
{
cout << *ite << " ";
}
cout << endl;
st.erase(2);//按值删除
cout << "迭代器的方式遍历结果为:";
for (ite = st.begin(); ite != st.end(); ite++)
{
cout << *ite << " ";
}
cout << endl;
//按区间删除
st.erase(st.find(4), st.end());
cout << "迭代器的方式遍历结果为:";
for (ite = st.begin(); ite != st.end(); ite++)
{
cout << *ite << " ";
}
cout << endl;
st.clear();//清空所有元素
cout << st.size() << endl;
return 0;
}
# include <iostream>
# include <algorithm>
#include<map>
using namespace std;
int main()
{
map<int, string> mp;
mp[1] = "zdy";
mp[2] = "wt";
mp[3] = "lhb";
mp[4] = "wzy";
map<int, string>::iterator ite;
ite = mp.find(1);
if (ite != mp.end())
cout << "find!" << ite->second << endl;
else
cout << "not find!"<<endl;
for (ite = mp.begin(); ite != mp.end(); ite++) cout << ite->second << " ";
cout << endl;
ite = mp.find(1);
mp.erase(ite);
for (ite = mp.begin(); ite != mp.end(); ite++) cout << ite->second << " ";
cout << endl;
mp.erase(mp.find(2), mp.find(4));
for (ite = mp.begin(); ite != mp.end(); ite++) cout << ite->second << " ";
cout << endl;
if (!mp.empty()) cout << mp.size() << endl;
else cout << "为空!" << endl;
return 0;
}
做题时,有时候会碰到需要列举一些数字所有排列方式的情况。这时候大家恐怕会无从下手,但如果使用next_permutation函数,就简单了。next_permutation函数在头文件<algorithm>中,作用是是生成给定序列的下一个较大排序,直到序列按降序排列为止。到这里还需要强调的一点是,如果你希望生成所有的排列方式,一定要先将序列按升序排列,这里可以与sort函数结合起来使用,先用sort升序排列,再调用next_permutation函数。
# include <iostream>
# include <algorithm>
using namespace std;
int main()
{
int a[] = { 3,1,2 };
sort(a, a + 3); //默认升序排列
do {
for (int i = 0; i < 3; i++) {
cout << a[i] << " ";
}
cout << endl;
} while (next_permutation(a, a + 3)); //第一个参数对应数组首,第二个参数对应数组尾
return 0;
}
# include <iostream>
# include <algorithm>
#include<queue>
using namespace std;
int main()
{
queue<int> que;
que.push(1);//尾插
que.push(2);
que.push(3);
cout << "队头元素:" << que.front() << endl;
cout << "队尾元素:" << que.back() << endl;
que.pop();
cout << "删除队头后的队头:" << que.front() << endl;
cout << "队列的大小:" << que.size() << endl;
if (!que.empty()) cout << "不为空!" << endl;
else cout << "为空!" << endl;
//队列不提供迭代器遍历
while (!que.empty())
{
cout << que.front() << " ";
que.pop();
}
return 0;
}
# include <iostream>
# include <algorithm>
#include<queue>
using namespace std;
int main()
{
priority_queue<int, vector<int>, greater<int> >que;
que.push(3);
que.push(1);
que.push(2);
while (!que.empty())
{
cout <<que.top() << " ";
que.pop();
}
cout << endl;
priority_queue<int, vector<int>, less<int> >que1;
que1.push(3);
que1.push(1);
que1.push(2);
while (!que1.empty())
{
cout << que1.top() << " ";
que1.pop();
}
return 0;
}
# include <iostream>
# include <algorithm>
#include<vector>
using namespace std;
int main()
{
vector<int> vec = {5,6};
vec.push_back(1);
vec.push_back(2);
vec.push_back(3);
vec.push_back(0);
//迭代器遍历
cout << "迭代器遍历结果:";
for (vector<int>::iterator ite = vec.begin(); ite != vec.end(); ite++) cout << *ite << " " ;
cout<< endl;
//下标遍历
cout << "下标遍历结果:" ;
for (int i = 0;i<vec.size(); i++) cout <<vec[i] << " ";
cout << endl;
cout << vec.front() << " " << vec.back() << endl;
sort(vec.begin(), vec.end());
cout << "sort结果:";
for (vector<int>::iterator ite = vec.begin(); ite != vec.end(); ite++) cout << *ite << " ";
cout << endl;
reverse(vec.begin(), vec.end());
cout << "翻转结果:";
for (vector<int>::iterator ite = vec.begin(); ite != vec.end(); ite++) cout << *ite << " ";
cout << endl;
vec.insert(vec.begin(), 9);
vec.insert(vec.begin()+3,2, 9);
cout << "插入结果为:";
for (vector<int>::iterator ite = vec.begin(); ite != vec.end(); ite++) cout << *ite << " ";
cout << endl;
vec.erase(vec.begin(), vec.begin() + 3);
cout << "删除结果为:";
for (vector<int>::iterator ite = vec.begin(); ite != vec.end(); ite++) cout << *ite << " ";
cout << endl;
return 0;
}
# include <iostream>
# include <algorithm>
#include<stack>
using namespace std;
int main()
{
stack<int> a;
a.push(1);
a.push(2);
a.push(3);
cout << a.size() << endl;
while (!a.empty()) {
cout << a.top() << " ";
a.pop();
}
return 0;
}
# include <iostream>
# include <algorithm>
#include<string>
using namespace std;
int main()
{
int a = 1;
cout << typeid(a).name() << endl;
string str = to_string(a);
cout << str << endl;
cout << typeid(stoi("2121")).name() << endl;
cout << typeid(stol("2121")).name() << endl;
cout << typeid(stoll("2121")).name() << endl;
cout << typeid(stof("2121.2")).name() << endl;
cout << typeid(stod("2121.2")).name() << endl;
return 0;
}
?