Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.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+中使用sort+;从第二个字符到最后一个字符对字符串进行排序_C++_Sorting - Fatal编程技术网

C++ 在C+中使用sort+;从第二个字符到最后一个字符对字符串进行排序

C++ 在C+中使用sort+;从第二个字符到最后一个字符对字符串进行排序,c++,sorting,C++,Sorting,我想对从第二个字符到最后一个字符的字符串进行排序。目前我可以按照我的要求对字符串进行排序,但是我想在C++中使用排序> /Cuff>函数来实现相同的 样本输入: san test van best 样本输出: san van test best 我目前的计划 for (int i1 = 0; i1 < n; ++i1) { for (int j1 = i1 + 1; j1 < n; ++j1) { temp5 = arr[j1].substr(1, arr

我想对从第二个字符到最后一个字符的字符串进行排序。目前我可以按照我的要求对字符串进行排序,但是我想在C++中使用<代码>排序> /Cuff>函数来实现相同的

样本输入:

san test van best
样本输出:

san van test best
我目前的计划

for (int i1 = 0; i1 < n; ++i1) {
    for (int j1 = i1 + 1; j1 < n; ++j1) {
        temp5 = arr[j1].substr(1, arr[j1].length() - 1);
        temp6 = arr[i1].substr(1, arr[i1].length() - 1);
        if (temp6.compare(temp5) > temp5.compare(temp6)) {
            a = arr[i1];
            arr[i1] = arr[j1];
            arr[j1] = a;
        }
    }
}
for(inti1=0;i1temp5.比较(temp6)){
a=arr[i1];
arr[i1]=arr[j1];
arr[j1]=a;
}
}
}

从示例I/O判断,您不希望只对字符串进行排序(这意味着它的字符)。你想对它进行标记,对子字符串进行排序,并将它们合并回一个新字符串。你的代码不会“对从2个字符到最后一个字符的字符串进行排序”。它试图忽略第一个字符对字符串数组进行排序,尽管我认为
if(temp6.compare(temp5)>temp5.compare(temp6))
是错误的。那么,你真正希望你的代码做什么?我有点不清楚你在做什么,你可能想考虑使用<代码> STD::Pultall排序> <代码>。我想对字符串数组进行排序,忽略从你的示例I/O中的第一个字符判断,你不只是想要排序一个字符串(这将意味着它的字符)。你想对它进行标记,对子字符串进行排序,并将它们合并回一个新字符串。你的代码不会“对从2个字符到最后一个字符的字符串进行排序”。它试图忽略第一个字符对字符串数组进行排序,尽管我认为
if(temp6.compare(temp5)>temp5.compare(temp6))
是错误的。那么,你真正希望你的代码做什么?我有点不清楚你在做什么,你可能想考虑使用<代码> STD::Pultall排序> 。我想对字符串数组进行排序,忽略第一个字符<代码> STD::StrigyVIEW 将避免复制。<代码>自动const和< /Cord>不会复制,但如果对C字符串集合进行排序,它就不会启用使用
substr()
。@下划线\d我认为“复制”Quimby实际上是指substr()复制字符串。虽然string_view的substr()没有复制。@Arty感谢您的解决方案,但我得到了以下错误:请求'arr'中的成员'begin',它是非类类型'std::uu cxx11::string[n]{aka std::u cxx11::basic_ustring[n]}std::sort(arr.begin(),arr.end(),[](auto const&l,auto const&r)我考虑过
std::stable\u sort
,但是单个测试用例不足以显示它的必要性。
std::string\u view
可以避免复制。
auto const&
也不会复制,但是如果对C字符串集合进行排序,它就不会启用
substr()
。@下划线\d我实际上是通过“复制”Quimby实际上是指substr()复制字符串。而字符串视图的substr()不复制。@Arty感谢您的解决方案,但我得到以下错误:请求“arr”中的成员“begin”,这是非类类型的“std::”cxx11::string[n]{aka std::”cxx11::basic_string[n]}std::sort(arr.begin(),arr.end(),[](auto-const&l,auto-const&r)我考虑了
std::stable_sort
,但单个测试用例不足以显示它的必要性。
std::sort(std::begin(arr), std::end(arr),
    [](std::string_view const & l, std::string_view const & r){
        return l.substr(1) < r.substr(1);
    }
);
#include <string_view>
#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>

int main() {
    std::string arr[] = {"san", "test", "van", "best"};
    std::sort(std::begin(arr), std::end(arr),
        [](std::string_view const & l, std::string_view const & r){
            return l.substr(1) < r.substr(1);
        }
    );
    for (auto const & s: arr)
        std::cout << s << " ";
}
san van test best