list和map的使用场景
#include <iostream>
using namespace std;
#include <list>
#include <string>
#include <map>
#include <algorithm>
//struct Bus
//{
// string m_station;
// int m_bus;
// Bus(string& s, int bus) {
// m_station = s;
// m_bus = bus;
// }
//};
//左值引用VS右值引用
//左值是在等号左边的变量,相当于string str1 = "服装城";中的str1
//右值是等号右边的值,相当于string str1 = "服装城"的"服装城"、string("黑大")
//&就是左值 &&就是右值
//Bus(string & s, int bus),这里的s因为只有一个&,所以他只能引用左值,就是str1这种:string str1 = "服装城";Bus bus1(str1, 111);,如果引用右值就会报错,说类型不匹配
//如果改成这样Bus(string && s, int bus),这里的s前面有两个&,所以他只能引用右值,就是string("黑大")这种:Bus bus1(string("黑大"), 111);
void show(pair<string, int> pr) {
cout << pr.first << " " << pr.second << endl;
}
int main() {
//————————————————————————一对一站点 list方法——————————————————————————
//list<Bus> lst;
//string str1 = "服装城";
//string str2 = "黑大";
//string str3 = "哈理工";
//Bus bus1(str1, 111);
//Bus bus2(str2, 122);
//Bus bus3(str3, 133);
//lst.push_back(bus1);
//lst.push_back(bus2);
//lst.push_back(bus3);
//string ss;
//cout << "请输入要查询的站点" << endl;
//cin >> ss;
//list<Bus>::iterator ite1 = lst.begin();
//while (ite1 != lst.end())
//{
// if ((*ite1).m_station == ss)
// {
// cout << (*ite1).m_bus << endl;
// break;
// }
// ite1++;
//}
//————————————————————————一对一站点 map方法——————————————————————————
//map<string, int> mm;
//mm["服装城"] = 111;
//mm["黑大"] = 122;
//mm["哈理工"] = 133;
//string ss;
//cout << "请输入要查询的站点" << endl;
//cin >> ss;
cout << mm[ss] << endl; //如果查询的站点不存在会自动加int=0的相应站点
::for_each(mm.begin(), mm.end(), &show);
优化
//if (mm.count(ss))
//{
// cout << mm[ss] << endl;
//}
//else {
// cout << "站点不存在" << endl;
//}
//::for_each(mm.begin(),mm.end(),&show);
//——————————————————————一对多站点 list-map方法————————————————————————
map<string, list<int>> mm;
list<int> lst1;
lst1.push_back(111);
lst1.push_back(122);
lst1.push_back(133);
mm["服装城"] = lst1;
list<int> lst2;
lst2.push_back(211);
lst2.push_back(222);
lst2.push_back(233);
mm["黑大"] = lst2;
list<int> lst3;
lst3.push_back(311);
lst3.push_back(322);
lst3.push_back(333);
mm["哈理工"] = lst3;
string ss;
cout << "请输入要查询的站点" << endl;
cin >> ss;
if (mm.count(ss))
{
list<int>::iterator ite2 = mm[ss].begin();
while (ite2 != mm[ss].end())
{
cout << *ite2 << " ";
ite2++;
}cout << endl;
}
else {
cout << "站点不存在" << endl;
}
system("pause");
return 0;
}
正反向迭代器
#include <iostream>
using namespace std;
#include <list>
#include <string>
#include <map>
#include <algorithm>
int main() {
list<int> lst;
lst.push_back(1);
lst.push_back(2);
lst.push_back(3);
lst.push_back(4);
//正向遍历/正向迭代器
list<int>::iterator ite1 = lst.begin();
while (ite1 != lst.end())
{
cout << *ite1 << " ";
ite1++;
}cout << endl;
//反向遍历/正向迭代器
list<int>::iterator ite2 = --lst.end(); //end指向的是结尾的下一个 所以要--
while (ite2 != lst.begin()) //begin是第一个 所以后面要补一条输出第一个的cout << *ite2
{
cout << *ite2 << " ";
ite2--;
}cout << *ite2 << endl;
//反向遍历/反向迭代器
list<int>::reverse_iterator ite3 = lst.rbegin();
//reverse 倒置 rbegin 倒置的头
//iterator 正向迭代器
//reverse_iterator 反向迭代器
while (ite3 != lst.rend()) //rbegin指向尾,rend指向头的前一个
{
cout << *ite3 << " ";
ite3++;
}cout << endl;
system("pause");
return 0;
}
正反向迭代器的转换
#include <iostream>
using namespace std;
#include <list>
#include <string>
#include <map>
#include <algorithm>
int main() {
list<int> lst;
lst.push_back(1);
lst.push_back(2);
lst.push_back(3);
lst.push_back(4);
list<int>::reverse_iterator ite3 = lst.rbegin();
//reverse 倒置 rbegin 倒置的头
//iterator 正向迭代器
//reverse_iterator 反向迭代器
while (ite3 != lst.rend()) //rbegin指向尾,rend指向头的前一个
{
cout << *ite3 << " ";
if ((*ite3) == 2)
{
//lst.erase(ite3); //ite3是反向的迭代器 不可以和正向迭代器一样操作
//lst.erase(ite3.base()); //反向变正向 后移一位 此时删除的是3而不是2 遍历输出结果为4 2 1
lst.erase(--ite3.base()); //遍历输出结果为4 3 1
break;
}
ite3++;
}cout << endl;
//遍历输出
list<int>::iterator ite2 = --lst.end();
while (ite2 != lst.begin())
{
cout << *ite2 << " ";
ite2--;
}cout << *ite2 << endl;
system("pause");
return 0;
}
转换图示
stack 栈
#include<iostream>
#include<stack>
using namespace std;
int main() {
stack<int> st;
st.push(1);
st.push(2);
st.push(3);
st.push(4); //添加
// 入栈 1 2 3 4
cout << st.empty() << endl;
//判断栈是否为空 如果为空返回ture 也就是1 不为空返回false 也就是0
//刚添加了4个呢 所以是1~
cout << st.size() << endl; //栈的大小
//不支持迭代器!
int count = st.size();
for (int i = 0; i < count; i++) //循环遍历
{
cout << st.top() << " "; //查看栈顶元素
st.pop(); //删除栈顶
}cout << endl;
// 出栈 4 3 2 1
cout<< st.empty()<<endl; //删完了当然是空啦 返回1~
system("pause");
return 0;
}
queue 队列
#include<iostream>
#include<queue>
using namespace std;
int main() {
queue<int> que;
que.push(1);
que.push(2);
que.push(3);
que.push(4);
//入队 1 2 3 4
cout << que.front() << endl; //返回首元素~
cout << que.back() << endl; //返回尾元素~
int count = que.size();
for (int i = 0; i < count; i++)
{
cout << que.front() << " "; //查看队列元素
que.pop(); //依次删除~
}cout << endl;
//出队 1 2 3 4
cout << que.empty() << endl; //队列为空
system("pause");
return 0;
}
win32项目小tips
贴图效果受到背景色影响怎么办?
-
原图为白底实物用黑底白物的屏蔽图
-
原图为黑底实物用白底黑物的屏蔽图