The longest connection path c ++ string find

Two ways

A: Direct sequencing on the line. For character string ">, <, == +" These operators are overloaded, and can be used directly.

 

#include <iostream>
using namespace std;
#include <string>

int main()
{
int num;
cin >> num;
string s[1000], temp;
for (int i = 0; i < num; i++) {
cin >> s[i];
}
for (int i = 0; i < num-1; i++) {
for (int j = 0; j < num-i-1; j++) {
if (s[j] > s[j+1]) {
temp = s[j];
s[j] = s[j+1];
s[j+1] = temp;
}
}
}
for (int i = 0; i < num; i++)
cout << s[i] << endl;

return 0;
}
二;采用STL封装的结构和函数


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
int input;
while (cin >> input)
{
string str;
vector<string> vs;
while (input--)
{
cin >> str;
vs.push_back(str);
}
sort(vs.begin(), vs.end());
vector<string>::iterator vit;
for (vit = vs.begin(); vit != vs.end(); vit++)
{
cout << *vit << endl;
}
}

return 0;
}

 

Guess you like

Origin www.cnblogs.com/xufeng123/p/12628896.html