简体   繁体   English

std :: sort无法对std :: vector的元素进行排序<std::string>

[英]std::sort fails to sort elements of a std::vector<std::string>

I have the following code: 我有以下代码:

#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <boost/foreach.hpp>
#include <boost/tokenizer.hpp>
#include <fstream>
#include <streambuf>

bool cmp(const std::string& lhs, const std::string& rhs) {
    return lhs < rhs;
}

int main(int argc, char **argv){
    /* USAGE: PROGRAM FILENAME DELIMITER */
    if (argc != 3){
        fprintf(stderr, "./program filename delimiter \n");
        exit(EXIT_FAILURE);
    }

    char *filename = argv[1];
    char *delimiter = argv[2];

    std::vector<std::string> vWords;
    std::vector<std::string> vWords_TMP;
    std::ifstream t(filename);
    std::string str((std::istreambuf_iterator<char>(t)),
                    std::istreambuf_iterator<char>());
    boost::char_separator<char> sep(delimiter);
    boost::tokenizer< boost::char_separator<char> > tokens(str, sep);
    BOOST_FOREACH (const std::string& t, tokens) {
        vWords.push_back(t);
    }
    vWords_TMP = vWords;
    for( std::vector<std::string>::const_iterator i = vWords.begin(); i != vWords.end(); ++i) std::cout << *i << '\n';
    std::sort(vWords_TMP.begin(), vWords_TMP.end());
    for( std::vector<std::string>::const_iterator i = vWords_TMP.begin(); i != vWords_TMP.end(); ++i) std::cout << *i << '\n';
}

However, when I run it, std::sort fails to sort the vector. 但是,当我运行它时,std :: sort无法对向量进行排序。 I input the following file: 我输入以下文件:

> FILE
UUUUUUUUUUUUUUUUUUUUU
AAAAAAAAAAAAAAAAAAAAA
KKKKKKKKKKKKKKKKKKKKK
BBBBBBBBBBBBBBBBBBBBB
YYYYYYYYYYYYYYYYYYYYY

Which should become: 应该变成:

AAAAAAAAAAAAAAAAAAAAA
BBBBBBBBBBBBBBBBBBBBB
KKKKKKKKKKKKKKKKKKKKK
UUUUUUUUUUUUUUUUUUUUU
YYYYYYYYYYYYYYYYYYYYY

But unfortunately the output is identical after the sort. 但不幸的是,排序后的输出是相同的。 Any ideas? 有任何想法吗?

Modify your vWords construction with a print statement: 使用打印语句修改您的vWords结构:

BOOST_FOREACH (const std::string& t, tokens) {
    vWords.push_back(t);
    std::cout << "pushing token: \"" << t << "\"" << std::endl;
}

And you'll notice that the entire file contents get pushed into your vector as a single string. 您会注意到,整个文件内容都作为单个字符串推入向量中。 Obviously sorting a single element won't change anything. 显然,对单个元素进行排序不会改变任何内容。

It's up to you to decide what should happen instead. 由您决定应该怎么做。

I'm assuming you mean that you want delimiter to be a newline. 我假设您的意思是希望delimiter为换行符。 The only way I'm aware of to pass a newline to your program, as it stands, on the command line is like this: 我知道在命令行上将换行符传递给您的程序的唯一方法是这样的:

$ ./a.out file  "
> "
pushing token: "UUUUUUUUUUUUUUUUUUUUU"
pushing token: "AAAAAAAAAAAAAAAAAAAAA"
pushing token: "KKKKKKKKKKKKKKKKKKKKK"
pushing token: "BBBBBBBBBBBBBBBBBBBBB"
pushing token: "YYYYYYYYYYYYYYYYYYYYY"
UUUUUUUUUUUUUUUUUUUUU
AAAAAAAAAAAAAAAAAAAAA
KKKKKKKKKKKKKKKKKKKKK
BBBBBBBBBBBBBBBBBBBBB
YYYYYYYYYYYYYYYYYYYYY
AAAAAAAAAAAAAAAAAAAAA
BBBBBBBBBBBBBBBBBBBBB
KKKKKKKKKKKKKKKKKKKKK
UUUUUUUUUUUUUUUUUUUUU
YYYYYYYYYYYYYYYYYYYYY

(and note that your program works as I think you want it to) (请注意,您的程序按我认为的方式运行)

The way I pass a newline as the command-line argument is that I have an opening " then the enter key, then closing " and enter again to run the command. 我将换行符作为命令行参数传递的方式是,先打开"然后输入Enter键,然后关闭" ,然后再次输入以运行命令。

I do not see the way you could pass carriage return as delimiter into your program, so you pass something else. 我看不到将回车符作为分隔符传递到程序中的方式,因此您传递了其他内容。 And you get all lines from the file with carriage returns as one string. 然后,您从文件中获取所有行,并用回车符作为一个字符串。 Sorting one string does not change anything, so you see the same output. 排序一个字符串不会改变任何内容,因此您将看到相同的输出。 To see if that the case change output loop to this: 查看是否将大小写更改输出循环为此:

for( std::vector<std::string>::const_iterator i = vWords_TMP.begin(); i != vWords_TMP.end(); ++i) 
    std::cout << "\"" << *i << "\"\n";

And check how many double quotes you see on the output. 并检查您在输出中看到多少个双引号。

The problem appears to be that you are reading all the lines of the file into one string. 问题似乎是您正在将文件的所有行读入一个字符串。 The actual sort algorithm itself works, as demonstrated here 实际的排序算法本身的工作原理,这表现在这里

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    typedef std::vector<std::string> Strings;
    Strings strings = { "world", "good bye", "hello", "aloha" };
    sort(strings.begin(), strings.end());
    for (Strings::iterator it = strings.begin(); it != strings.end(); ++it) cout << *it << "\n";
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM