码迷,mamicode.com
首页 > 编程语言 > 详细

c++字串的连接最长路径查找

时间:2020-04-03 21:59:35      阅读:85      评论:0      收藏:0      [点我收藏+]

标签:==   names   std   str   字符   运算   space   temp   ios   

两种方式

一:直接排序就行了。对于字符串“>、<、==、+”这些运算符都被重载了,可以直接用。

 

#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;
}

 

c++字串的连接最长路径查找

标签:==   names   std   str   字符   运算   space   temp   ios   

原文地址:https://www.cnblogs.com/xufeng123/p/12628896.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!