对字符串向量进行排序,C++具有要放在末尾的特殊值
Sorting a string vector in C++ with a special value to place at the end
我正在尝试创建一个函数,该函数使用以下条件对字符串向量进行排序:
所有字符串 = "NULL" 必须转到向量的末尾并从那里减少。其余字符串必须保持其顺序。
例如给出:
{"Potato", "NULL", "NULL", "Charmander" , "Spaghetti", "NULL"}
输出应为:
{"Potato","Charmander","Spaghetti","NULL","NULL","NULL"}
我尝试过这个,但它没有完全起作用:
bool comp(string i, string j){
if(i=="NULL"){return i>j;}
if (j=="NULL") {return i<j;}
提前致谢
您可以执行以下两项操作之一:
- 首先处理"NULL",然后以我们通常的幼稚方式对其他字符串进行排序
- 使用您定义的更复杂的顺序对所有字符串进行排序
首先处理"空">
标准库有一个"分区"算法,它将所有匹配某个条件的元素移动到字符串的末尾。
std::vector<string> vec {
"Potato", "NULL", "NULL", "Charmander" , "Spaghetti", "NULL"
};
auto is_not_null = [](const std::string& s) { return s != "NULL"; }
auto nulls_start = std::partition(vec.begin(), vec.end(), is_not_null);
auto non_nulls_end = nulls_start;
std::sort(vec.begin(), non_nulls_end);
使用复杂比较进行排序
std::vector<string> vec {
"Potato", "NULL", "NULL", "Charmander" , "Spaghetti", "NULL"
};
auto comparator =
[](const std::string& lhs, const std::string& rhs)
{
return rhs == "NULL" or lhs <= rhs;
};
std::sort(vec.begin(), vec.end(), comparator);
请注意此处的比较与comp()
函数之间的差异。比较器回答问题"我得到的第一个字符串应该在第二个字符串之前吗?"——而你的comp()
函数只是没有给出符合你要求的答案。
您可以使用分区算法:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argc, const char * argv[]) {
vector<string> vec {
"Potato", "NULL", "NULL", "Charmander" , "Spaghetti", "NULL"
};
partition(begin(vec), end(vec), // Partition the values
[](const string& s) { return s != "NULL"; });
copy(begin(vec), end(vec), ostream_iterator<string>{cout, " "});
cout << endl;
return 0;
}
// RESULT: Potato Spaghetti Charmander NULL NULL NULL
注意:如果您需要保持相对顺序,请改用stable_partition。
您可以编写自己的函数版本,将一些字符串放在末尾,即:
namespace myAlgo {
template<typename ForwardIter, class T >
ForwardIter moveToEnd(ForwardIter first, ForwardIter last, const T& value) {
if (first == last) {
return first;
}
ForwardIter fasterFirst = first;
//Shift strings that do not match value to the left in stable manner
while (++fasterFirst != last) {
if (*fasterFirst != value) {
*++first = *fasterFirst;
}
}
ForwardIter pivot = first;
//Fill rest with value
while (first != last) {
*++first = value;
}
return pivot;
}
}
然后只是:
myAlgo::moveToEnd(vec.begin(), vec.end(), "NULL");
相关文章:
- 在for循环中使用auto vs decltype(vec.size())来处理字符串的向量
- 构造<int>具有 2 个字符串文字的向量
- 如何将元素添加到向量的字符串位置
- 将向量解析<string>为字符串
- C ++中的StringStream有助于使用向量从字符串中提取逗号分隔的整数,而不是空格分隔的整数,为什么?
- 在 C++11 中,如何查找并返回以给定字符串开头的字符串向量中的所有项?
- 如何使用向量字符串令牌构建表达树
- 将 argv 复制到新的全局向量字符串
- 如何读取控制台输入并将其存储到向量字符串中
- 向量字符串push_back在C 中不起作用
- C char数组和向量字符串
- 可以直接初始化向量<字符串 *> 而不使用 new 吗?
- 迭代向量字符串的字符(C++禁止指针和整数之间的比较)
- 比较两个向量字符串的相似性C++
- 我有麻烦将字符串放入向量字符串
- 如何从一个向量字符串得到一个字符串的联合集
- 函数返回向量/字符串,但不返回数组
- 向量向量字符串固定大小
- c++向量字符串排序不工作
- C++将字符串数组[]复制到向量<字符串>