Jump to content

  • Log In with Google      Sign In   
  • Create Account


Really simple c++ question


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 numegil   Members   -  Reputation: 142

Like
0Likes
Like

Posted 23 November 2009 - 12:36 AM

Hi, The answer to this question is probably something really obvious, but Google is failing me on this one: Say I have a pointer to C++ string called "it". How do I create a newstring with the same contents as "it"? (it is a vector iterator). I can't use strcpy in any way since I'm not allowed to use c strings at all for this project. string newstring; newstring = *it; This code works within the context of the function, however causes massive crashes later on in the program. I know the problem lies in the way I'm assigning the value, since if I do the below instead, the program runs perfectly: newstring = "Hello world"; Help please? -Numegil

Sponsor:

#2 Arjan B   Members   -  Reputation: 621

Like
0Likes
Like

Posted 23 November 2009 - 12:46 AM

When you modify a vector, all of it's iterators become invalid. I think this might be the problem ^^.

#3 mrbastard   Members   -  Reputation: 1573

Like
0Likes
Like

Posted 23 November 2009 - 12:50 AM

have a look at std::copy

#4 numegil   Members   -  Reputation: 142

Like
0Likes
Like

Posted 23 November 2009 - 12:58 AM

Quote:
Original post by Arjan B
When you modify a vector, all of it's iterators become invalid. I think this might be the problem ^^.


This was exactly the problem. Thank you for saving me hours of head-banging on keyboard!

#5 Zahlman   Moderators   -  Reputation: 1682

Like
0Likes
Like

Posted 23 November 2009 - 03:22 AM

Quote:
Original post by Arjan B
When you modify a vector, all of it's iterators become invalid. I think this might be the problem ^^.


Using .erase() will keep iterators before the erasure point valid, since memory isn't reallocated. Of course, it's generally a bad idea to store iterators anyway, so normally there is only ever one iterator for a given container at a time :)

(Edited for correctness.)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS