Vectors in C++ are behaving differently in Windows and Ubuntu

143 views Asked by At

I have to compare two strings which are stored in a vector. Comparison works fine in Windows which uses mingw-g++ compiler(version 4.4.1).

But when I try to do the same in Ubuntu which is running g++ version 4.7.2 I am getting weird problems. I listed them below:

  1. When I try to print the elements individually, they are giving correct output and both strings are same.

  2. But when I try to compare them using == operator or strcmp() it is saying that they are not equal even though they are same.

  3. When I try to print the elements which are compared above some string is replacing first string's value. The code is given below. Ideally the if loop shouldn't be executed, but it is still executing and printing some garbage value instead of v1[i].

vector<string> v1 = r1->GetSchema().GetAttrTypes();
vector<string> v2 = r2->GetSchema().GetAttrTypes();
for(i=0; i<v2.size();i++)
    if(v1[i] != v2[i])
        cout << v1[i] << " " << v2[i] << " awdsd" << endl;

I don't know what to search for these kind of errors. I am taking strings from same file and storing in vectors in both Windows and Ubuntu.

EDIT: I am attaching part of the code here. The function right side returns vector of strings. I can't paste above classe's code as it is very big code.

2

There are 2 answers

1
Kiran On

Well, everything was just fine, except file from which I am reading input string. Since I created that file on windows, it's End of line is different(\r\n) where as in Ubuntu it is (\n).

In Ubuntu I had to convert the text file I am reading input Unix Mode. After that everything is fine.

I don't know how I missed this small point.

Thank you for your input.

0
Paul Evans On

The problem has to be in:

r1->GetSchema().GetAttrTypes();

This function must be mucking about with how it's std::string's are created. Can you show us the code?