开发者

std::sort() C++ not working but its so simple, why :( 1D array

开发者 https://www.devze.com 2023-02-27 09:35 出处:网络
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 do

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开发者_如何学C();
}
else
{
    cout << "Cant open the file" << endl;
}

for(j = 0; j < i; j++)
{
    cout << numbers[j] << endl;
}

system("pause");
}

what do you guys think?


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();


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 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;
}


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.

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.


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:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号