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

时间:2013-11-10 07:56:07

标签: c++ sorting

我有以下代码:

#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';
}

然而,当我运行它时,std :: sort无法对向量进行排序。我输入以下文件:

> FILE
UUUUUUUUUUUUUUUUUUUUU
AAAAAAAAAAAAAAAAAAAAA
KKKKKKKKKKKKKKKKKKKKK
BBBBBBBBBBBBBBBBBBBBB
YYYYYYYYYYYYYYYYYYYYY

应该成为:

AAAAAAAAAAAAAAAAAAAAA
BBBBBBBBBBBBBBBBBBBBB
KKKKKKKKKKKKKKKKKKKKK
UUUUUUUUUUUUUUUUUUUUU
YYYYYYYYYYYYYYYYYYYYY

但遗憾的是,排序后输出相同。有什么想法吗?

3 个答案:

答案 0 :(得分:1)

使用print语句修改vWords构造:

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

您会注意到整个文件内容作为单个字符串被推送到您的向量中。显然,对单个元素进行排序不会改变任何内容。

由你来决定应该发生什么。

我假设您的意思是希望delimiter成为换行符。我知道在命令行中将新行传递给程序的唯一方法是:

$ ./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

(并注意您的程序按我认为的那样工作)

我作为命令行参数传递换行符的方式是我有一个开始"然后是回车键,然后关闭"并再次输入以运行命令。

答案 1 :(得分:0)

我没有看到您将回车作为分隔符传递到程序中的方式,因此您传递了其他内容。并且您从文件中获取所有行,并将回车符作为一个字符串。对一个字符串进行排序不会改变任何内容,因此您可以看到相同的输出。 要查看案例是否更改输出循环到此:

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

并检查您在输出中看到多少双引号。

答案 2 :(得分:0)

问题似乎是您正在将文件的所有行读取为一个字符串。实际的排序算法本身有效,如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;
}