C++ 通过Qt中的程序在文本文件中排序
我正在使用Qt5.2.1,我制作了一个程序,在文本文件中存储一些值(如名称、编号、其他详细信息…)。我想根据姓名来安排这些信息 像这样的 A Alan 24140239一些其他信息 艾薇儿64376334一些其他信息 B bob 532523一些其他信息 等等C++ 通过Qt中的程序在文本文件中排序,c++,qt,sorting,user-interface,C++,Qt,Sorting,User Interface,我正在使用Qt5.2.1,我制作了一个程序,在文本文件中存储一些值(如名称、编号、其他详细信息…)。我想根据姓名来安排这些信息 像这样的 A Alan 24140239一些其他信息 艾薇儿64376334一些其他信息 B bob 532523一些其他信息 等等 我该怎么做?我几乎没有编程经验,所以请尽量保持简单:)这个例子既快又脏,但应该给你一个想法。 我使用了一个结构,因为所有元素默认为C++ 我想Qt有一些方便的函数来完成这些工作 #include <algorithm> #in
我该怎么做?我几乎没有编程经验,所以请尽量保持简单:)这个例子既快又脏,但应该给你一个想法。 我使用了一个结构,因为所有元素默认为C++ 我想Qt有一些方便的函数来完成这些工作
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
struct my_data_t
{
std::string m_name;
int m_number;
// default constructor
my_data_t( std::string name="", int number = 0) :
m_name(name), m_number(number) {}
// less than operator
bool operator <(my_data_t b) const ;
};
// less than operator implementation ( I might have the const not quite right
// i struggle with const correctness.
bool my_data_t::operator<(my_data_t b) const
{
int32_t t = m_name.compare( b.m_name);
if( t > 0 ) {
return false;
}
else if( t < 0 )
{
return true;
}
else if( m_number > b.m_number )
{
return true;
}
// add other tests
// refer to std::sort
return false;
}
int main(int argc, char **argv )
{
std::vector<my_data_t> data;
my_data_t d3("Tom", 29) ;
my_data_t d4("Jill", 22);
data.push_back( my_data_t("Tom", 32) );
data.push_back( my_data_t("Jack", 31) );
data.push_back( d3 );
data.push_back( d4 );
std::cout << "Before sorting \n" << std::endl;
for( size_t i = 0; i < data.size(); ++i )
{
std::cout << "name, number:\t" <<
data[i].m_name <<
" " <<
data[i].m_number << std::endl;
}
std::cout << std::endl;
std::sort( data.begin(), data.end() );
std::cout << "After sorting \n" << std::endl;
for( size_t i = 0; i < data.size(); ++i )
{
std::cout << "name, number:\t" <<
data[i].m_name <<
" " <<
data[i].m_number << std::endl;
}
return 0;
}
#包括
//用cygwin编译
$g++-o sort.cpp
//运行示例
$./sort.exe
分拣前
姓名、号码:汤姆32
姓名、号码:杰克31
姓名、号码:汤姆29
姓名、号码:吉尔22
分类后
姓名、号码:杰克31
姓名、号码:吉尔22
姓名、号码:汤姆32
姓名、号码:Tom 29问题是什么?我该如何回答?我需要的是朝正确的方向轻轻推一推。。。一步一步的指导非常棒:我认为你需要提供更多的细节。您的问题是如何对文本文件进行排序?请设置一个包含名称、编号和其他详细信息的结构。将单个元素放入这些元素的向量中。为该结构编写小于运算符,并使用std::sort。“小于”运算符应根据您的structure@riklund是的,这是我的问题,但它有点棘手(至少对我来说),我希望字母表下的排序元素(如我在上面的问题中所示)超过一吨…我现在有一个模糊的想法如何做到这一点…让我试试这个。又是唐克斯。你是个救生员:D