#include<iostream>
#include<deque>
#include<list>
using namespace std;
template<typename T>
void print(T t1,T t2)
{
while(t1!=t2)
cout<<*t1++<<" ";
cout<<endl;
}
int main(){
int num[]={1,2,3,4,5,6,7,8};
size_t size=sizeof(num)/sizeof(int);
list<int> lst(num,num+size);
deque<int> deq1;
deque<int> deq2;
list<int>::const_iterator iter=lst.begin();
while(iter!=lst.end())
{
if(*iter%2==0)
deq1.push_back(*iter);
else
deq2.push_back(*iter);
iter++;
}
print(lst.begin(),lst.end());
print(deq1.begin(),deq1.end());
print(deq2.begin(),deq2.end());
return 0;
}
/*判断vector<int>与list<int>容器包含元素是否完全相同*/
#include<iostream>
#include<list>
#include<vector>
using namespace std;
int main()
{
int a[]={1,2,3};
size_t size=sizeof(a)/sizeof(int);
vector<int> vec(a,a+size);
//list<int> lst(a+1,a+size);
list<int> lst(a,a+size);
vector<int>::const_iterator it1=vec.begin();
list<int>::const_iterator it2=lst.begin();
while(it1!=vec.end() && it2!=lst.end())
{
if((*it1-*it2)!=0)
break;
it1++;
it2++;
}
if(it1==vec.end() && it2==lst.end())
cout<<"相同!"<<endl;
else
cout<<"不相同!"<<endl;
return 0;
}
/*将vector容器里的偶数值删除,将list容器里的奇数值删除*/
#include<iostream>
#include<list>
#include<vector>
using namespace std;
template <typename T>
void print(T t1,T t2){
while(t1!=t2)
cout<<*t1++<<" ";
cout<<endl;
}
int main()
{
int ai[]={0,1,1,2,3,5,8,13,21,55,89};
size_t size=sizeof(ai)/sizeof(int);
vector<int> vec(ai,ai+size);
list<int> lst(ai,ai+size);
vector<int>::iterator it1=vec.begin();
print(vec.begin(),vec.end());
while(it1!=vec.end())
{
if(*it1%2==0)
it1=vec.erase(it1);
else
it1++;
}
print(vec.begin(),vec.end());
list<int>::iterator it2=lst.begin();
while(it2!=lst.end())
{
if(*it2%2)
it2=lst.erase(it2);
else
it2++;
}
print(lst.begin(),lst.end());
return 0;
}
#include<iostream>
#include<vector>
#include<list>
#include<string>
using namespace std;
template <typename T>
void print(T t1,T t2){
while(t1!=t2)
cout<<*t1++<<" ";
cout<<endl;
}
int main(){
char *str[]={"Hello","World","china","shanghai"};
size_t size=sizeof(str)/sizeof(char *);
list<char *> lst(str,str+size);
print(lst.begin(),lst.end());
vector<string> vec;
vec.assign(lst.begin(),lst.end());
return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str="Hello World!";
string::iterator it=str.begin();
while(it!=str.end())
{
if(*it>'a'-1 && *it<'z'+1)
*it=*it-32;
it++;
}
cout<<str<<endl;
return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str="Hello World!";
string::iterator it=str.begin();
while(it!=str.end())
{
if(*it>='A'&& *it<='Z')
it=str.erase(it);
else
it++;
}
cout<<str<<endl;
return 0;
}
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{
char str[]="Hello World";
size_t size=sizeof(str)/sizeof(char);
vector<char> vec(str,str+size);
string s(vec.begin(),vec.end());
cout<<s<<endl;
return 0;
}
#include<iostream>
#include<string>
#include<vector>
using namespace std;
template <typename T>
void print(T t1,T t2){
while(t1!=t2)
cout<<*t1++<<" ";
cout<<endl;
}
int main(){
string str="ab2c3d7r4e6";
string numbers("0123456789");
string word("abcdefghijklmnopqrstuvwxyz");
vector<char> vec1;
vector<char> vec2;
string::size_type pos=0;
while((pos=str.find_first_of(numbers,pos))!=string::npos)
{
vec1.push_back(str[pos]);
pos++;
}
pos=0;
while((pos=str.find_first_of(word,pos))!=string::npos)
{
vec2.push_back(str[pos]);
pos++;
}
print(vec1.begin(),vec1.end());
print(vec2.begin(),vec2.end());
return 0;
}
#include<iostream>
#include<string>
#include<vector>
using namespace std;
/*编程计算sentence中有多少个单词,并指出其中最长和最短的单词。*/
/*如果有多个最长或最短的单词,则将他们全部输出。*/
template <typename T>
void print(T t1,T t2){
while(t1!=t2)
cout<<*t1++<<endl;
}
int main(){
string line1="We were her pride of 10 she named us:";
string line2="Benjamin, Phoenix, the Prodigal";
string line3="and perspicacious pacific Suzanne ";
string sentence=line1+' '+line2+' '+line3;
string separators(" :,");
vector<string> longWords;
vector<string> shortWords;
string::size_type startpos=0,endpos=0;
string::size_type maxLen,minLen;
bool flag=true;
while((endpos=sentence.find_first_of(separators,startpos))!=string::npos)
{
if(endpos-startpos!=0)
{
string word(sentence,startpos,endpos-startpos);
//cout<<word<<"\t"<<startpos<<"\t"<<endpos<<endl;
//vec.push_back(word);
if(flag)/*第一个单词*/
{
maxLen=minLen=word.size();
longWords.push_back(word);
shortWords.push_back(word);
flag=false;
}else{
if(word.size()==maxLen)//和最长单词一样长
longWords.push_back(word);
if(word.size()>maxLen)//比最长单词还长
{
longWords.clear();
longWords.push_back(word);
maxLen=word.size();
}
if(word.size()==minLen)//和最短单词一样长
shortWords.push_back(word);
if(word.size()<minLen)//比最短单词还短
{
shortWords.clear();
shortWords.push_back(word);
minLen=word.size();
}
}
}
startpos=endpos+1;//寻找下一个单词
}
print(longWords.begin(),longWords.end());
cout<<"*********"<<endl;
print(shortWords.begin(),shortWords.end());
return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main(){
string q1("When lilacs last in the dooryard bloom'd");
string q2("The child is father of the man");
string sentence;
sentence.assign(q2.begin(),q2.begin()+13);
sentence.append(q1.substr(q1.find("in")),0,15);
cout<<sentence<<endl;
return 0;
}
#include<iostream>
#include<string>
using namespace std;
string greet(string form,string lastname,string title,string::size_type pos,int length)
{
form.replace(form.find("Daisy"),strlen("Daisy"),lastname);
form.replace(form.find("Ms"),strlen("Ms"),title,pos,length);
return form;
}
int main(){
string generic1("Dear Ms Daisy:");
string generic2("MrsMsMissPeople");
string lastname("AnnaP");
string salute=greet(generic1,lastname,generic2,5,4);
cout<<salute<<endl;
return 0;
}
#include<iostream>
#include<string>
#include<stack>
using namespace std;
int main(){
string word;
stack<string> wordstack;
while(cin>>word)
wordstack.push(word);
while(wordstack.empty()==false)
{
cout<<wordstack.top()<<"->";
wordstack.pop();
}
cout<<"NULL"<<endl;
return 0;
}
#include<iostream>
#include<string>
#include<stack>
using namespace std;
int main(){
string exp="1*(2+3)+5";
cout<<exp<<endl;
stack<char> sexp;
string::iterator it=exp.begin();
while(it!=exp.end())
{
if(*it!=')')
sexp.push(*it);
else
{
while(sexp.top()!='(' &&sexp.empty()==false)
sexp.pop();
if(sexp.empty())
{
cout<<"括弧不匹配!";
}else{
sexp.pop();
sexp.push('@');
}
}
it++;
}
while(sexp.empty()==false)
{
cout<<sexp.top()<<"->";
sexp.pop();
}
cout<<"NULL"<<endl;
return 0;
}