基本上,我需要读取一个文本文件并将它们插入到列表中的ascending order
上。如何对整数进行排序并将它们放在列表中
该文本文件为,
4 1 9 11 0 15 23 2 7 8 17 21.
和我需要把他们的列表上像
问题说
将第一项插入空白列表中。 //哪个将是4
对于每一个连续的文件:
If it is smaller than the first item, insert it at position 0.
Otherwise, scan the list from the beginning, looking for the first
item whose value is greater than the current value, and insert
the new item before that one.
我认为我需要通过比较对这些问题进行排序,但我不能提出清晰的想法。你可以帮我吗 ?
(因为教授是用他自己的IList.h
和IList.cpp
文件,我可以使用所有的功能是插入和删除。)
=================== ================================================== ====
我试过了,
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char* argv[])
{
ifstream inf(argv[1]);
IList t;
int i1;
int i2;
int i3;
int i4;
int i5;
int i6;
int i7;
int i8;
int i9;
int i10;
int i11;
int i12;
//It is reading each integers from the text file and name it i1, i2, i3..
// The text file is..
// 4 1 9 11 0 15 23 2 7 8 17 21.
// i1 is going to be 4 i1 = 4
inf >> i1 >> i2 >> i3 >> i4 >> i5 >> i6 >> i7 >> i8 >> i9 >> i10 >> i11
>> i12;
//I inserted the first value which is 4
t.insert(i1, 0);
// comparison will start from here..
// when i2 is smaller than i1, we are putting them on the left.
if ((i2 < i1))
{
t.insert(i2, 0);
}
// when i2 is greater than i1, we are putting them on the right.
if ((i2 > i1))
{
t.insert(i2, 1);
}
使用数组('的std :: array',甚至C数组)将解决*所有*您的问题:) – Rakete1111
[或使用'的std :: list']( http://en.cppreference.com/w/cpp/container/list)的说明似乎建议。 – user4581301
使用列表的任何具体原因?你应该使用'std :: set' –
cplusplusrat