基本上,我需要讀取一個文本文件並將它們插入到列表中的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