c++ - 如何用单词排列向量中的每个字符?
问题描述
我想像这样安排每个单词:
输入
5
viguo
lkjhg
tyujb
asqwe
cvbfd
输出
aeqsw
bcdfv
bjtuy
ghjkl
giouv
我想从你那里得到一些想法。谢谢!
解决方案
您需要应用该算法std::sort
两次。
第一个用于向量的每个元素,第二个用于向量本身。
这是一个演示程序。
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
int main()
{
std::vector<std::string> v =
{
"viguo", "lkjhg", "tyujb", "asqwe", "cvbfd"
};
for ( const auto &s : v ) std::cout << s << ' ';
std::cout << '\n';
for ( auto &s : v ) std::sort( std::begin( s ), std::end( s ) );
std::sort( std::begin( v ), std::end( v ) );
for ( const auto &s : v ) std::cout << s << ' ';
std::cout << '\n';
return 0;
}
它的输出是
viguo lkjhg tyujb asqwe cvbfd
aeqsw bcdfv bjtuy ghjkl giouv
推荐阅读
- asp.net - 如何使用 Owin 实现授权码流程
- javascript - Google Sheets Script - 循环遍历所有工作表,并将某一列的所有值添加到数组中,然后计算唯一值
- android - 将字符串用于 Android 意图 putExtra() 方法
- django - Django 关系“personindustry”不存在
- r - R:将值转换为具有多个唯一标识符的data.frame中的变量(列)
- vb.net - 使用VB将字符串发送到串口
- deep-learning - 如何为 CoLA 使用 HuggingFace nlp 库的 GLUE
- sql - 使用存储过程自动插入自上次插入以来的秒数
- python - 如何在python中将视频转换为m4v格式而不保存到文件
- excel - “运行时错误‘-2147221040 (800401d0)’有时会在 Excel 从 2010 年升级到 2016 年之后发生