.返回和排序功能 我是C++初学者,为了一个任务,我被要求对超级英雄的文本文件进行排序,并将它们输出到另一个文本文件,例如

.返回和排序功能 我是C++初学者,为了一个任务,我被要求对超级英雄的文本文件进行排序,并将它们输出到另一个文本文件,例如,c++,sorting,C++,Sorting,Unsorted.txt 死池8 凤凰城9号 蟾蜍4 禧年3 按字母和数字顺序 我曾尝试对每行字符串使用.back进行数字排序,尽管它只是不接受它并返回一个错误(我已将其包含在代码中),同时它很乐意完整地写入控制台,而cout尝试写入文本文件只会导致最后一行字符串,例如 蟾蜍4 (我所有的错误都被注释掉了,目前只按字母顺序排序) 我对问别人同样的问题有点偏执,但我一直找不到任何能解决我问题的方法 #include <iostream> #include <string>

Unsorted.txt

死池8

凤凰城9号

蟾蜍4

禧年3

按字母和数字顺序

我曾尝试对每行字符串使用.back进行数字排序,尽管它只是不接受它并返回一个错误(我已将其包含在代码中),同时它很乐意完整地写入控制台,而cout尝试写入文本文件只会导致最后一行字符串,例如

蟾蜍4

(我所有的错误都被注释掉了,目前只按字母顺序排序)

我对问别人同样的问题有点偏执,但我一直找不到任何能解决我问题的方法

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <fstream>
#include <limits>
using namespace std;
// Empty vector holding names from file
vector<string> names;
string word;
string number;
string filename;
string sortChoice;
string lastChar;
bool alphaSortFinished = false; //bool added to prevent unnecessary looping
bool sortFinished = false;

void sortNumerically()
{
//word = word.back;         returns this error
/*Error 1   error C3867:         'std::basic_string<char,std::char_traits<char>,std::allocator<char>>::back': function call missing argument list; use 
'&std::basic_string<char,std::char_traits<char>,std::allocator<char>>::back' to create a pointer to member  
d:\visual studio 2013\assessment one mdu118\strings, classes assessment one\source.cpp  20  1   Strings, Classes Assessment One*/


cout << "Please specify the file you would like to open\n" << endl;
cin >> filename;

// Read names from specified file
ifstream inFile(filename);

while (!inFile && sortFinished == false)
{
    cout << "Unable to open file\n";
    inFile.close();
    sortNumerically();
}

while (getline(inFile, word)) //get lines of the string, store them in string word;
{
    names.push_back(word);
}

sort(names.begin(), names.end());

// Loop to print names
for (size_t i = 0; i < names.size(); i++)
{
    //ofstream writeToFile;
    //writeToFile.open("NumericalSort.txt");
    //writeToFile << names[i] << '\n';
    //writeToFile.close();
    cout << names[i] << '\n';
}
sortFinished = true;
inFile.close();
}
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
//包含文件名的空向量
载体名称;
字符串字;
字符串编号;
字符串文件名;
字符串排序选择;
字符串lastChar;
bool alphaSortFinished=false//添加bool以防止不必要的循环
bool-sortFinished=false;
按顺序无效()
{
//word=word.back;返回此错误
/*错误1错误C3867:'std::basic_string::back':函数调用缺少参数列表;使用
“&std::basic_string::back”创建指向成员的指针
d:\visual studio 2013\assessment one mdu118\strings,classes assessment one\source.cpp 20 1 strings,classes assessment one*/
cout文件名;
//从指定的文件读取名称
ifstream-infle(文件名);
而(!infle&&sortFinished==false)
{

无法填充字符串的
向量

vector<string> names;
ifstream inFile(filename);
copy(istream_iterator<string>(inFile), istream_iterator<string>(), back_inserter(names));
按数字排序

sort(names.begin(), names.end(), cmpr());
cmpr
是一个自定义比较器,用于比较字符串的数字部分

要获取字符串的数字部分,请使用

int num = stoi(s.substr(s.find_last_of('_') + 1));
C++11

sort(names.begin(), names.end(), [](const string & a, const string & b) {
    int ia = stoi(a.substr(a.find_last_of('_') + 1));
    int ib = stoi(b.substr(b.find_last_of('_') + 1));
    return ia < ib;
});
排序(names.begin()、names.end()、[](常量字符串&a、常量字符串&b){
int ia=stoi(a.substr(a.find_last_of(“'u')+1));
intib=stoi(b.substr(b.find_last_of(“'u')+1));
返回ia

请参见演示

将文件中的名称读取到
std::vector

现在,


<比较器应该以2个字符串作为参数,比较字符串的数字部分。

我不认为使用更少会对字符串进行数值排序。@ JAGANNATI我正在更新它。无论如何,谢谢你指出。@ EMONEY666,如果你的问题解决了,也许你可以接受答案。谢谢。应用程序,因此格式不好。
sort(names.begin(), names.end(), [](const string & a, const string & b) {
    int ia = stoi(a.substr(a.find_last_of('_') + 1));
    int ib = stoi(b.substr(b.find_last_of('_') + 1));
    return ia < ib;
});
sort(names.begin(), names.end()).
sort(names.begin(), names.end(), comparator)