Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 关于在C++;_C++_Sorting - Fatal编程技术网

C++ 关于在C++;

C++ 关于在C++;,c++,sorting,C++,Sorting,如何仅根据每个单词的长度对字符串中的单词进行稳定排序,使最长的单词排在第一位,最短的单词排在最后(但相同长度的单词保持其相对于彼此的原始顺序) 例如: 输入: 结果输出: sj sa df sd fd a r e w f d s a v c x z 最简单的解决方案是将字符串拆分为一个字符串集合,然后对该集合进行排序,并再次将结果连接为一个字符串:您可以使用自定义比较器,使用字符串的大小将其馈送到std::stable_sort以对字符串集合进行排序: // Your original str

如何仅根据每个单词的长度对字符串中的单词进行稳定排序,使最长的单词排在第一位,最短的单词排在最后(但相同长度的单词保持其相对于彼此的原始顺序)

例如: 输入:

结果输出:

sj sa df sd fd a r e w f d s a v c x z

最简单的解决方案是将字符串拆分为一个字符串集合,然后对该集合进行排序,并再次将结果连接为一个字符串:您可以使用自定义比较器,使用字符串的大小将其馈送到
std::stable_sort
以对字符串集合进行排序:

// Your original string
std::string your_string = "sj a sa df r e w f d s a v c x z sd fd";

// Split the string
std::istringstream iss(your_string);
std::vector<std::string> words{
    std::istream_iterator<std::string>(iss),
    std::istream_iterator<std::string>()
};

// Stable sort by size
std::stable_sort(std::begin(words), std::end(words), [](const std::string& lhs, const std::string& rhs) {
    return lhs.size() > rhs.size();
});

// Merge the results back with spaces
std::ostringstream oss;
std::copy(
    std::begin(words), std::end(words),
    std::ostream_iterator<std::string>(oss, " ")
);
your_string = oss.str();
//您的原始字符串
std::string your_string=“sj a sa dfr e w f d s a v c x z sd fd”;
//拆线
std::istringstream iss(您的_字符串);
向量词{
std::istream_迭代器(iss),
std::istream_迭代器()
};
//按大小排序
std::stable_sort(std::begin(words),std::end(words),[](常量std::string&lhs,常量std::string&rhs){
返回lhs.size()>rhs.size();
});
//将结果合并回空格
std::ostringstream oss;
复制(
标准::开始(单词),标准::结束(单词),
std::ostream_迭代器(oss,“”)
);
您的_string=oss.str();
当然,这个算法不是最优的。为了避免大量计算,您可以直接存储字符串向量并使用它,而不是将单词存储在空格分隔的字符串中。在次优情况中:我们从原始字符串中复制了许多内容,而有些内容可能会在
单词
向量中来回移动(不确定)。事实上,我们去掉这些空间,把它们加回去,这就清楚地表明,我们做的比我们能做的还要多


另外,请注意,最后一个字符串将有一个额外的尾随空格,您可以选择使用
pop_back

删除该空格,并向我们展示一些您尝试过的代码。对不起,我不明白您在说什么。英语是一门难学的语言!请找一个比你更流利的人来写这个问题。也许可以举个例子。我试过用C语言,但在很多论坛或博客上我都得到了一些提示和代码。。他们在对字符串或
std::string
的向量中的字符进行排序时做到了这一点。我只是从流中初始化
words
:std::vector words{std::istream\u iterator(iss),std::istream\u iterator()}`@JerryCoffin哦,对了,它让事情变得简单了。谢谢:)
// Your original string
std::string your_string = "sj a sa df r e w f d s a v c x z sd fd";

// Split the string
std::istringstream iss(your_string);
std::vector<std::string> words{
    std::istream_iterator<std::string>(iss),
    std::istream_iterator<std::string>()
};

// Stable sort by size
std::stable_sort(std::begin(words), std::end(words), [](const std::string& lhs, const std::string& rhs) {
    return lhs.size() > rhs.size();
});

// Merge the results back with spaces
std::ostringstream oss;
std::copy(
    std::begin(words), std::end(words),
    std::ostream_iterator<std::string>(oss, " ")
);
your_string = oss.str();