This should be really simple, I have found that the first argument is the array name, the second is the size of the array + array name. However, it doesnt seem to be sorting at all, in fact its not doing anything, well not writing anything on the console, am I doing somethin silly?
这应该很简单,我发现第一个参数是数组名,第二个是数组+数组名的大小。然而,它似乎根本就没有排序,实际上它没有做任何事情,也没有在控制台上写任何东西,我在做些什么傻事?
int main()
{
readFromFile();
system("pause");
return 0;
}
void readFromFile()
{
string line;
int i = 0;
int j;
ifstream file("ACW2_data.txt");
if(file.is_open())
{
getline(file, line);
while (!file.eof())
{
file >> numbers[i];
i++;
int elements = sizeof(numbers) / sizeof(numbers[0]);
**sort(numbers, numbers + elements);**
}
file.close();
}
else
{
cout << "Cant open the file" << endl;
}
for(j = 0; j < i; j++)
{
cout << numbers[j] << endl;
}
system("pause");
}
what do you guys think?
你们有什么感想?
4 个解决方案
#1
2
while (!file.eof())
{
file >> numbers[i];
i++;
int elements = sizeof(numbers) / sizeof(numbers[0]);
**sort(numbers, numbers + elements);**
}
file.close();
to
while (file >> numbers[i])
{
++i;
}
sort( numbers, numbers + i );
file.close();
or
std::vector<your_int_type> numbers;
your_int_type tmp;
while (file >> tmp)
{
numbers.push_back(tmp);
}
std::sort( numbers.begin(), numbers.end() );
file.close();
#2
2
Edit: For the moment, I'm assuming numbers
was an array of int. If not, well, I'll hope you can figure out what to do...
编辑:目前,我假设数字是一个int数组。如果没有,那么,我希望你能弄明白该做什么......
int main() {
std::ifstream file("ACW2_data.txt");
std::vector<int> numbers;
file.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::copy(std::istream_iterator<int>(file),
std::istream_iterator<int>(),
std::back_inserter(numbers));
std::sort(numbers.begin(), numbers.end());
std::copy(numbers.begin(), numbers.end(),
std::ostream_iterator<int>(std::cout, "\n"));
return 0;
}
#3
0
First, where is numbers
defined, and what is its type?
首先,数字定义在哪里,它的类型是什么?
Second, the number of elements in numbers
is i
at every iteration of the while loop, so you do not need to calculate it.
其次,在while循环的每次迭代中,数字中的元素数量是i,因此您不需要计算它。
Third, why are you sorting numbers
every time you insert a new element? Why not insert all the elements, and then sort once. After the while loop that is.
第三,为什么每次插入新元素时都要对数字进行排序?为什么不插入所有元素,然后排序一次。在while循环之后。
#4
0
My wild guess, since you are not showing the important detail of how numbers
is declared, is that it is a pointer, and the sizeof
trick is failing to calculate the allocated size. It is better to use a template based sizeof like:
我猜测,因为你没有显示如何声明数字的重要细节,它是一个指针,并且sizeof技巧无法计算分配的大小。最好使用基于模板的sizeof,如:
template <typename T, int N>
int array_size( T (&)[N] ) {
return N;
}
or:
template <typename T, int N>
char (&array_size_impl( T(&)[N] ))[N];
#define ARRAY_SIZE( x ) sizeof( array_size_impl( x ) )
As those will trigger compile time errors if a pointer is used, instead of silently failing and yielding unexpected results as the sizeof(x)/sizeof(x[0])
trick.
因为如果使用指针,这些将触发编译时错误,而不是静默失败并产生意外的结果作为sizeof(x)/ sizeof(x [0])技巧。
#1
2
while (!file.eof())
{
file >> numbers[i];
i++;
int elements = sizeof(numbers) / sizeof(numbers[0]);
**sort(numbers, numbers + elements);**
}
file.close();
to
while (file >> numbers[i])
{
++i;
}
sort( numbers, numbers + i );
file.close();
or
std::vector<your_int_type> numbers;
your_int_type tmp;
while (file >> tmp)
{
numbers.push_back(tmp);
}
std::sort( numbers.begin(), numbers.end() );
file.close();
#2
2
Edit: For the moment, I'm assuming numbers
was an array of int. If not, well, I'll hope you can figure out what to do...
编辑:目前,我假设数字是一个int数组。如果没有,那么,我希望你能弄明白该做什么......
int main() {
std::ifstream file("ACW2_data.txt");
std::vector<int> numbers;
file.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::copy(std::istream_iterator<int>(file),
std::istream_iterator<int>(),
std::back_inserter(numbers));
std::sort(numbers.begin(), numbers.end());
std::copy(numbers.begin(), numbers.end(),
std::ostream_iterator<int>(std::cout, "\n"));
return 0;
}
#3
0
First, where is numbers
defined, and what is its type?
首先,数字定义在哪里,它的类型是什么?
Second, the number of elements in numbers
is i
at every iteration of the while loop, so you do not need to calculate it.
其次,在while循环的每次迭代中,数字中的元素数量是i,因此您不需要计算它。
Third, why are you sorting numbers
every time you insert a new element? Why not insert all the elements, and then sort once. After the while loop that is.
第三,为什么每次插入新元素时都要对数字进行排序?为什么不插入所有元素,然后排序一次。在while循环之后。
#4
0
My wild guess, since you are not showing the important detail of how numbers
is declared, is that it is a pointer, and the sizeof
trick is failing to calculate the allocated size. It is better to use a template based sizeof like:
我猜测,因为你没有显示如何声明数字的重要细节,它是一个指针,并且sizeof技巧无法计算分配的大小。最好使用基于模板的sizeof,如:
template <typename T, int N>
int array_size( T (&)[N] ) {
return N;
}
or:
template <typename T, int N>
char (&array_size_impl( T(&)[N] ))[N];
#define ARRAY_SIZE( x ) sizeof( array_size_impl( x ) )
As those will trigger compile time errors if a pointer is used, instead of silently failing and yielding unexpected results as the sizeof(x)/sizeof(x[0])
trick.
因为如果使用指针,这些将触发编译时错误,而不是静默失败并产生意外的结果作为sizeof(x)/ sizeof(x [0])技巧。