1. 程式人生 > >vector<string>怎麽用

vector<string>怎麽用

names esp while length main bool std tor i+1

//兩種排序方法

#include<iostream>
#include<string>
#include<vector>
using namespace std;
vector<string>vec;//定義個一個字符串容器

bool lexicographically(){
int i=0;
while(i<vec.size()-1){//vec.size() //返回容器中實際數據的個數。
if(vec[i].compare(vec[i+1])>0){
return 0;
}
i++;
}
return 1;
}

bool lengths(){
int i=0;
while(i<vec.size()-1){
if(vec[i].size()>vec[i+1].size())
{
return 0;
}
i++;
}
return 1;
}
int main(){
int i=0,n;
string str;
bool r1,r2;
cin>>n;
while(i<n){
cin>>str;
vec.push_back(str);//把字符串str壓進容器,vec.pop_back();//取出容器中最後一個
i++;
}
r1=lexicographically();
r2=lengths();
if(r1&&r2){
cout<<"both";
}
else if(r1&&(!r2)){
cout<<"lexicographically";
}
else if((!r1)&&r2){
cout<<"lengths";
}
else{
cout<<"none";
}
return 0;
}

vector<string>怎麽用