📄 比较算法.cpp
字号:
#include <algorithm>
#include <vector>
#include <list>
#include <iostream>
using namespace std;
template<typename Container>
void populateContainer(Container& cont)
{
int num;
while(true){
cout<<"Enter a number (0 to quit): ";
cin>>num;
if(num == 0){
break;
}
cont.push_back(num);
}
}
int main(int argc,char *argv[])
{
vector<int> myVector;
list<int> myList;
cout<<"Populate the vector:\n";
populateContainer(myVector);
cout<<"Populate the list:\n";
populateContainer(myList);
if(myList.size()<myVector.size()){
cout<<"Sorry,the list is not long enough.\n";
return 0;
}
if(equal(myVector.begin(),myVector.end(),myList.begin())){
cout<<"The two containers have equal elements\n";
}else{
pair<vector<int>::iterator,list<int>::iterator> miss =
mismatch(myVector.begin(),myVector.end(),myList.begin());
cout<<"The first mismatch is at position "
<<miss.first - myVector.begin()<<". The vector has value "
<<*(miss.first)<<" and the list has value "<<*(miss.second)
<<endl;
}
if(lexicographical_compare(myVector.begin(),myVector.end(),myList.begin(),myList.end())){
cout<<"The vector is lexicographically first.\n";
}else{
cout<<"The list is lexicographically first.\n";
}
getchar();
getchar();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -