Jump to content
  • Advertisement
Sign in to follow this  
roberts

Vector erase - delete at specified position - crazy

This topic is 2251 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi
I have a problem with deleting an item in my vector container. I used it many times but never had this problem.

I have a simple class:

CBuffer // simplified just for illustration
{
float value;
CString str;
}

CString s;
vector<CBuffer> v;

[color=#0000FF][color=#0000FF]for([color=#0000FF][color=#0000FF]int i=0; i<3;i++)
{
s.Format([color=#A31515][color=#A31515]"%.2f", i);
CBuffer b;
b.setString(str);
b.setValue(([color=#0000FF][color=#0000FF]float)i);
v.push_back(b);
}

Here I have a simple loop storing into container v the following:
[color=#FF0000]v[0].value=0.0; v[0].str=0.0;
v[1].value=1.0; v[1].str=1.0;
v[2].value=2.0; v[2].str=2.0;

Now when I delete at pos=0 obviously this below should be removed:

v[0].value=0.0; v[0].str=0.0;

std::vector<CBuffer>::iterator iter = v.begin();
v.erase(iter+pos);

I get this below!! IT doesnt make sense. v[0].value=0.0 was deleted which is good but then for some reason v[2].str=2.0 was also deleted. at the end?? Why is that?

SEE this output: THIS IS DRIVING ME MAD!!! Whats going on?

[color=#FF0000]v[0].value=1.0; v[0].str=0.0;
v[1].value=2.0; v[1].str=1.0;


This should be like below! but its not
v[0].value=1.0; v[0].str=1.0;
v[1].value=2.0; v[1].str=2.0;

Can someone explain this? I deleted at pos 0 my own type object CBuffer with value=0.0 str=0.0 from container so I expect the container to shrink to be size=2 which is correct but the values are incorrect mixed up.

Share this post


Link to post
Share on other sites
Advertisement
There are a lot of different issues that could cause that kind of problem. Try posting a complete, but minimal code sample that demonstrates your problem. Someone should ideally be able to simply copy and paste your code into their compiler and see the issue as you describe it. My personal guess is that one of your classes doesn't implement the assignment operator properly, but that's just a guess without being able to see more complete code.

Share this post


Link to post
Share on other sites
What is CString? If it's actually a class holding text, why are you assigning floats to it?

Do you check (i.e. print out or view in the debugger) the values of the array before you erase the element? Your assignment might be wrong.
Perhaps, "str.Format("%.2f", i);[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]" where you tell it you are expecting a float, but pass it an int?[/font]

Or perhaps your CString class doesn't properly handle re-assignment?

Can you show the entire code, including where you print out the values? You might be accidentally printing them wrong.

Share this post


Link to post
Share on other sites
Hi Thank you for your replies: CString is a Microsoft class to store strings of text. my CBuffer class looks like this:

[size="2"][color="#0000ff"][size="2"][color="#0000ff"]class

[size="2"]CBuffer
[size="2"]{
[size="2"][color="#0000ff"][size="2"][color="#0000ff"]public
[size="2"]CBuffer();
[size="2"]~CBuffer();
[size="2"][color="#0000ff"][size="2"][color="#0000ff"]float[size="2"] getValue();
[size="2"]CString getColumn();
[size="2"][color="#0000ff"][size="2"][color="#0000ff"]void[size="2"] setColumn( [size="2"][color="#0000ff"][size="2"][color="#0000ff"]const[size="2"] CString &col);
[size="2"][color="#0000ff"][size="2"][color="#0000ff"]void[size="2"] setMatch( [size="2"][color="#0000ff"][size="2"][color="#0000ff"]bool[size="2"] flag );
[size="2"][color="#0000ff"][size="2"][color="#0000ff"]void[size="2"] setValue([size="2"][color="#0000ff"][size="2"][color="#0000ff"]float[size="2"] val);

[size="2"][color="#0000ff"][size="2"][color="#0000ff"]private
[size="2"]CString m_csColumn;
[size="2"][color="#0000ff"][size="2"][color="#0000ff"]float[size="2"] m_fValue;
[size="2"][color="#0000ff"][size="2"][color="#0000ff"]bool[size="2"] m_bMatch;
[size="2"]};



[size="2"][size="2"][color="#0000ff"][size="2"][color="#0000ff"]void [size="2"]CDBaseBarcodeScanDlg::addItem( CBuffer item )
[size="2"][size="2"]{
[size="2"][size="2"]m_vItem.push_back(item);
[size="2"][size="2"]}

[size="2"][size="2"][color="#0000ff"][size="2"][color="#0000ff"]bool [size="2"]CDBaseBarcodeScanDlg::deleteRecordAt( [size="2"][color="#0000ff"][size="2"][color="#0000ff"]int[size="2"] pos )
[size="2"][size="2"]{
[size="2"][size="2"][color="#0000ff"][size="2"][color="#0000ff"]int[size="2"] size = m_vItem.size();
[size="2"][size="2"]std::vector<CBuffer>::iterator iter = v.begin();

[size="2"][size="2"][color="#0000ff"][size="2"][color="#0000ff"]if[size="2"](pos<size)
[size="2"][size="2"]{
[size="2"][size="2"]m_vItem.erase(iter+pos);

[size="2"][size="2"][color="#0000ff"][size="2"][color="#0000ff"]return [size="2"][color="#0000ff"][size="2"][color="#0000ff"]true[size="2"];
[size="2"][size="2"]}

[size="2"][size="2"][color="#0000ff"][size="2"][color="#0000ff"]return [size="2"][color="#0000ff"][size="2"][color="#0000ff"]false[size="2"];
[size="2"][size="2"]}

Share this post


Link to post
Share on other sites
Try reproducing the bug in an empty project, with as small an example as possible.

Like so:


#include <vector>
#include <string>
#include <sstream>
std::string IntToStdString(int number)
{
std::stringstream stream;
stream << number;
return stream.str();
}

struct Buffer
{
std::string str;
float value;
};

int main(int argc, char *argv[])
{
std::string str;
std::vector<Buffer> vector;
for(int i = 0; i < 3; i++)
{
Buffer buffer;
buffer.str = IntToStdString(i);
buffer.value = (float)i;
vector.push_back(buffer);
}

std::cout << "Before:\nString\tValue" << std::endl;

for(int i = 0; i < vector.size(); i++)
{
std::cout << vector.str << " - " << vector.value << std::endl;
}

vector.erase(vector.begin() + 0);

std::cout << "\nAfter:\nString\tValue" << std::endl;
for(int i = 0; i < vector.size(); i++)
{
std::cout << vector.str << " - " << vector.value << std::endl;
}

return 0;
}


Output:

[color=#006400]

[font=courier new,courier,monospace]Before:[/font]

[color=#006400][font=courier new,courier,monospace]String Value[/font]

[color=#006400][font=courier new,courier,monospace]0 - 0[/font]

[color=#006400][font=courier new,courier,monospace]1 - 1[/font]

[color=#006400][font=courier new,courier,monospace]2 - 2[/font]

[color=#006400][font=courier new,courier,monospace]After:[/font]

[color=#006400][font=courier new,courier,monospace]String Value[/font]

[color=#006400][font=courier new,courier,monospace]1 - 1[/font]

[color=#006400][font=courier new,courier,monospace]2 - 2[/font]

Nothing wrong when I run it. So use this as a base example, and slowly start re-introducing additional complexity and see where it goes wrong. First try replacing std::string with CString. I'll tell you this though: std::vector isn't broken. wink.png

Before you do that, however, I'd again recommend combing slowly through where you assign the values and where you print them out for debugging, since you can easily make a mistake there.
Maybe you are accidentally assigning your formatted string to the buffer _before_ formatting the string, each iteration accidentally assigning the previous iteration's formatted value?

Share this post


Link to post
Share on other sites
Thank you very much for all your help. I managed to find the problem as you suggested. There was an assigment operator CBuffer not properly done. some variables were missing and hence there was a problem with deleting memory when using erase. Anyway thank you both for your help. :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!