Thread: question about vector

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    98

    question about vector

    can u explain me why in this vector:
    Code:
    vector<string> v;
    string s = "abc";
    v.push_back(s);
    if i want v[0] to be empty i have to do
    Code:
    v.clear();
    v[0].~string();
    and not only one of them?

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    What do you mean? Why isn't this doing what you want:
    Code:
    string s = "abc";
    
    vector<string> v;
    v.push_back(s);
    
    cout<<v[0]<<endl;
    
    v.clear();
    
    cout<<v[0]<<"<-----"<<endl;
    cout<<v.size()<<endl;
    Last edited by 7stud; 04-28-2005 at 03:36 AM.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    98
    have u tried compiling your code? here's the output:
    Code:
    abc
    abc<-----
    0
    and that's my point, i want v[0] to be empty after clearing it

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Quote Originally Posted by hiya
    have u tried compiling your code?
    Yes, I compile all my code before posting it.
    Here is the output with MS VC++6:
    Code:
    abc
    <-----
    0
    Press any key to continue
    What compiler are you using? Can you access an element at index position 1 after calling clear(). You may also want to try:

    v.erase(v.begin(), v.end());

    however the results should be the same.
    Last edited by 7stud; 04-28-2005 at 04:15 AM.

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    98
    im using dev-c++.
    however the results should be the same.
    Yep...
    it's strange that your compiler removing it and mine don't.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    What about:

    Can you access an element at index position 1 after calling clear()
    e.g.:
    Code:
    string s = "abc";
    string s2 ="xyz";
    
    vector<string> v;
    v.push_back(s);
    v.push_back(s2);
    
    cout<<v[1]<<endl;
    
    v.clear();
    
    cout<<v[1]<<"<-----"<<endl;
    I also wonder what is at v[0] after clear(): "abc" or "xyz"?
    Last edited by 7stud; 04-28-2005 at 05:41 AM.

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    98
    after clear:
    v[1] = xyz
    v[0] = abc
    it seems that the only thing clear() does is changing the size to 0

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by hiya
    can u explain me why in this vector:
    Code:
    vector<string> v;
    string s = "abc";
    v.push_back(s);
    if i want v[0] to be empty i have to do
    Code:
    v.clear();
    v[0].~string();
    and not only one of them?
    I can explain. Your code is completely invalid and therefore completely unpredictable.

    v.clear();

    This line empties the vector. After this, v[0] doesn't exist anymore. Any access to it is invalid and yields undefined results - that it happens to yield exactly what was there previously is just one interpretation of "undefined". It might also crash your app. (And will do so on VC++7.1.)

    v[0].~string();

    This line destructs the string at v[0]. After this, the string object mustn't be used anymore, and that includes access.

    What you want is
    v[0].clear();
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    You should post the exact code you are using to get those results, and then people can comment on why that is happening.

    Does the same thing happen with integers?

  10. #10
    Registered User
    Join Date
    Apr 2005
    Posts
    98
    CornedBee,
    v[0].clear(); will remove the string v[0] contains, but v[0] will remain undeleted.
    what i can do is
    Code:
    for(int i=0; i<v.size(); i++)
         v[i].clear();
    v.clear();
    but what happens when the vector is for ints? i can't use clear() then

  11. #11
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Maybe...

    Code:
    v.erase(v.begin());
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  12. #12
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Code:
    for(int i=0; i<v.size(); i++)
         v[i].clear();
    v.clear();
    is the same as v.clear(). What you need to do is use v.erase(v.begin());

    To read more about this I recommend http://www.cppreference.com
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Huh?

    Just what do you want, exactly?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #14
    Registered User
    Join Date
    Apr 2005
    Posts
    98
    Shakti, that's what i thought but when i use
    Code:
    for(int i=0; i<v.size(); i++)
         v[i].clear();
    v.clear();
    v[0] is empty, and when i use
    Code:
    v.erase(v.begin());
    v[0] is "abc" (or any value i give him in the beginning)

  15. #15
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Using [] on a vector to get access to a specific element does not perform any checking to make sure you are pointing to a valid element. Try using the at member function to access that first element after erasing/clearing it and you should get a runtime-error or "abnormal porgram termination" error when you run the program. It's up to you to not attempt to access elements of a vector that do not exist.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying Array to Vector Question
    By bengreenwood in forum C++ Programming
    Replies: 1
    Last Post: 06-01-2009, 03:00 PM
  2. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 12:34 PM
  3. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  4. Question about view vector, position, and matrix speed
    By Silvercord in forum Game Programming
    Replies: 1
    Last Post: 02-03-2003, 12:37 PM
  5. Operators for 3D Vector Mathematics
    By Anarchist in forum C++ Programming
    Replies: 10
    Last Post: 01-31-2003, 07:33 PM