这个应该还是上次的代码,来自queue
估计没有掌握的原因是一直在看别人怎么写而没有自己动手,当初觉得自己会了其实还是一知半解。
故,给自己出到小题目,练一练:
要求:输入1 1 2 3 5 8 13 21
一开始是照着上面那个改的,既然是照着改的就肯定没出BUG,只不过效果……不是我想要的
代码如下:
#include<bits/stdc++.h>
using namespace std;
int main()
{
queue<int> feib;
int a;
do
{
cin>>a;
feib.push(a);
}while(a);
while(!feib.empty())
{
cout<<' '<<feib.front();
feib.pop();
}
cout<<'\n';
return 0;
}
这个输入无比诡异,必须要输入0以后才肯输出,后来我问了一下才知道,原因是:“while(a);”
经过改正
#include<bits/stdc++.h>
using namespace std;
int main()
{
queue<int> feib;
int a;
for(int i=0;i<8;i++)
{
cin>>a;
feib.push(a);
}
while(!feib.empty())
{
cout<<' '<<feib.front();
feib.pop();
}
cout<<'\n';
return 0;
}
这样看起来正常多了。
②然后还是上次写过的stack
// stack::push/pop
#include <iostream> // std::cout
#include <stack> // std::stack
int main ()
{
std::stack<int> mystack;
for (int i=0; i<5; ++i) mystack.push(i);
std::cout << "Popping out elements...";
while (!mystack.empty())
{
std::cout << ' ' << mystack.top();
mystack.pop();
}
std::cout << '\n';
return 0;
}
输出: 4 3 2 1 0
还是练一练:
要求输入一个数n,输出在1到n-2的所有数,并要求逆向输出
代码如下:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
stack <int> feib;
while(cin>>n)
{
if(n==0)
break;
for(int i=1;i<2*n-1;i++)
{
feib.push(i);
}
while(!feib.empty())
{
cout<<' '<<feib.top();
feib.pop();
}
}
return 0;
}
练习2:输入两个数,交换两个数并输出(我记得,原来正经的做法都是int a,b;然后cin<<a<<b;最后再cout>>a>>b;来着)
顺便,这个代码应该不能直接提交AC,毕竟最后有一个空格问题没有处理
代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
stack <int> resort;
int a;
for(int i=0;i<2;i++)
{
cin>>a;
resort.push(a);
}
while(!resort.empty())
{
cout<<resort.top()<<' ';
resort.pop();
}
return 0;
}
③终于写到vector了
毕竟我之前所有试图用vector写的都报错了,还是得克服一下,硬着头皮写╮(╯▽╰)╭
// inserting into a vector
#include <iostream>
#include <vector>
int main ()
{
std::vector<int> myvector (3,100);
std::vector<int>::iterator it;
it = myvector.begin();
it = myvector.insert ( it , 200 );
myvector.insert (it,2,300);
// "it" no longer valid, get a new one:
it = myvector.begin();
std::vector<int> anothervector (2,400);
myvector.insert (it+2,anothervector.begin(),anothervector.end());
int myarray [] = { 501,502,503 };
myvector.insert (myvector.begin(), myarray, myarray+3);
std::cout << "myvector contains:";
for (it=myvector.begin(); it<myvector.end(); it++)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
输出:
myvector contains: 501 502 503 300 300 400 400 200 100 100 100
练习:
要求:已知数列:1 1 2 3 ,要求插入两个数字,5 和 8
代码如下:
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector <int> feib;
vector <int>::iterator fei;
fei=feib.begin();
fei=feib.insert(fei,8);
feib.insert(fei,1,5);
int insfeib[]={1,1,2,3};
feib.insert(feib.begin(),insfeib,insfeib+4);
for(fei=feib.begin();fei<feib.end();fei++)
cout<<' '<<*fei;
cout<<'\n';
return 0;
}
输出: 1 1 2 3 5 8
介于我比较好奇那些个数字是干什么的,所以一个一个改了改看效果:
首先,先改了feib.insert(fei,1,5);把1改为2:
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector <int> feib;
vector <int>::iterator fei;
fei=feib.begin();
fei=feib.insert(fei,8);
feib.insert(fei,2,5);///stack的名字.insert(迭代器的名字,想让插入的数据重复几次,插入的数据)
int insfeib[]={1,1,2,3};
feib.insert(feib.begin(),insfeib,insfeib+4);
for(fei=feib.begin();fei<feib.end();fei++)
cout<<' '<<*fei;
cout<<'\n';
return 0;
}
输出: 1 1 2 3 5 5 8
然后,又试图改了一下feib.insert(feib.begin(),insfeib,insfeib+4);把4改为3
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector <int> feib;
vector <int>::iterator fei;
fei=feib.begin();
fei=feib.insert(fei,8);
feib.insert(fei,1,5);///stack的名字.insert(迭代器的名字,想让插入的数据重复几次,插入的数据)
int insfeib[]={1,1,2,3};
feib.insert(feib.begin(),insfeib,insfeib+3);///vector的名字.insert(vector的名字.begin(),数组的名字,数组的名字+数组出现的位数)
for(fei=feib.begin();fei<feib.end();fei++)
cout<<' '<<*fei;
cout<<'\n';
return 0;
}
输出
1 1 2 5 8
然后,还是这一行,我把begin改为back结果报错(╯‵□′)╯︵┻━┻
最后,我把8和5的位置对调:
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector <int> feib;
vector <int>::iterator fei;
fei=feib.begin();
fei=feib.insert(fei,5);
feib.insert(fei,1,8);///stack的名字.insert(迭代器的名字,想让插入的数据重复几次,插入的数据)
int insfeib[]={1,1,2,3};
feib.insert(feib.begin(),insfeib,insfeib+4);
for(fei=feib.begin();fei<feib.end();fei++)
cout<<' '<<*fei;
cout<<'\n';
return 0;
}
我觉得,这里把栈的特点完全表示出来了——输出: 1 1 2 3 8 5
我们先把5按进栈,然后再把8按进去,所以在出来的时候,5在8之后出现,可是这明明是vector(╯‵□′)╯︵┻━┻,我也不晓得为什么,如果想明白了,会在底下接着更新的。
④最后再写一个map吧
// map::insert (C++98)
#include <iostream>
#include <map>
int main ()
{
std::map<char,int> mymap;
// first insert function version (single parameter):
mymap.insert ( std::pair<char,int>('a',100) );
mymap.insert ( std::pair<char,int>('z',200) );
std::pair<std::map<char,int>::iterator,bool> ret;
ret = mymap.insert ( std::pair<char,int>('z',500) );
if (ret.second==false) {
std::cout << "element 'z' already existed";
std::cout << " with a value of " << ret.first->second << '\n';
}
// second insert function version (with hint position):
std::map<char,int>::iterator it = mymap.begin();
mymap.insert (it, std::pair<char,int>('b',300)); // max efficiency inserting
mymap.insert (it, std::pair<char,int>('c',400)); // no max efficiency inserting
// third insert function version (range insertion):
std::map<char,int> anothermap;
anothermap.insert(mymap.begin(),mymap.find('c'));
// showing contents:
std::cout << "mymap contains:\n";
for (it=mymap.begin(); it!=mymap.end(); ++it)
std::cout << it->first << " => " << it->second << '\n';
std::cout << "anothermap contains:\n";
for (it=anothermap.begin(); it!=anothermap.end(); ++it)
std::cout << it->first << " => " << it->second << '\n';
return 0;
}
输出
element 'z' already existed with a value of 200
mymap contains:
a => 100
b => 300
c => 400
z => 200
anothermap contains:
a => 100
b => 300
练习:
要求:
输出序号和几个单词
代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
map<int,string> words;
words.insert(pair<int,string>(1,"iterator"));
words.insert(pair<int,string>(2,"fatigue"));
words.insert(pair<int,string>(3,"iterator"));
words.insert(pair<int,string>(4,"iterator"));
pair<map<int,string>::iterator,bool> ret;
ret=words.insert(pair<int,string>(5,"fatigue"));
map<int,string>::iterator it=words.begin();
cout<<"The words contains:"<<'\n';
for(it=words.begin();it!=words.end();++it)
cout<<it->first<<"=>>"<<it->second<<'\n';
return 0;
}
输出:
The words contains:
1=>>iterator
2=>>fatigue
3=>>iterator
4=>>iterator
5=>>fatigue
听说map的功能很强大,所以我又皮了一下:
#include<bits/stdc++.h>
using namespace std;
int main()
{
map<int,string> words;
words.insert(pair<int,string>(1,"iterator"));
words.insert(pair<int,string>(2,"fatigue"));
words.insert(pair<int,string>(3,"iterator"));
words.insert(pair<int,string>(2,"iterator"));
pair<map<int,string>::iterator,bool> ret;
ret=words.insert(pair<int,string>(2,"fatigue"));
map<int,string>::iterator it=words.begin();
cout<<"The words contains:"<<'\n';
for(it=words.begin();it!=words.end();++it)
cout<<it->first<<"=>>"<<it->second<<'\n';
return 0;
}
输出:
The words contains:
1=>>iterator
2=>>fatigue
3=>>iterator
果然,能去重并排序……
而且还拒绝后面相同序号赋不同的字符串
|