deleting elements of a vector

This is a discussion on deleting elements of a vector within the C++ Programming forums, part of the General Programming Boards category; Dear All My programm looks (shortened version) like this Code: vector <int> k, v; for (i=...) { . for (j=...) ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    50

    Post deleting elements of a vector

    Dear All
    My programm looks (shortened version) like this
    Code:
    vector <int> k, v;
    for (i=...)
    {
       .
       for (j=...)
       {.
         .
         k.push_back(something)
         v.push_back(something)
        }
        //I want to empty here elements of v, k so I can use k
           and v anew//
    }
    My question is this possible without using loops?
    Thank you

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,802
    Use the clear member function:
    Code:
    v.clear();
    k.clear();
    "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

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    50

    member functions

    Is there any member function that sorts out a vector or finds index of its greatest element?

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,802
    Quote Originally Posted by strickey
    Is there any member function that sorts out a vector
    Yes, the sort function:
    Code:
    #include <algorithm>
    #include <vector>
    
    ...
    
    std::vector<int> v;
    
    ...
    
    std::sort(v.begin(),v.end());
    Quote Originally Posted by strickey
    or finds index of its greatest element?
    Yes, the max_element function:
    Code:
    #include <algorithm>
    #include <vector>
    #include <iostream>
    
    ...
    
    std::vector<int> v;
    
    ...
    
    std::cout << "Index of biggest element is: " << std::max_element(v.begin(),v.end()) << std::endl;
    std::cout << "Actual biggest element is: " << *std::max_element(v.begin(),v.end()) << std::endl;
    [edit]Just for clarification, the sort and max_element functions are not member functions.[/edit]
    Last edited by hk_mp5kpdw; 02-09-2005 at 06:54 AM.
    "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

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    50

    One more question

    Thank you !!!
    May I ask if there is a member function that trace elements of same values and discards them all but one?
    Thank you one more time

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Warm recommendation:
    http://msdn.microsoft.com/library/de...thmmembers.asp

    There's no built-in function that does it in general, but std::unique does it for sorted vectors, so you can just sort the vector by calling std::sort and then use std::unique to remove all duplicates, and finally std::vector::erase to tell the container that elements were removed.
    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

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,802
    Quote Originally Posted by strickey
    Thank you !!!
    May I ask if there is a member function that trace elements of same values and discards them all but one?
    Thank you one more time
    The easiest solution to this is to first sort the container and then call the unique function (not a member function) in combination with the vector's erase member function:
    Code:
    #include <vector>
    #include <algorithm>
    
    ...
    
    std::vector<int> v;
    
    ...
    
    std::sort(v.begin(),v.end());
    v.erase(std::unique(v.begin(),v.end()),v.end());
    The reason for combining unique and erase is that unique shuffles the duplicates towards the end of the container and does not remove them. The unique function returns the new logical end of the container (the point where all those duplicates get shuffled to). So you then call the erase member function to remove the elements starting from that logical end position to the true end of the container.

    This method will of course destroy the original ordering of elements in the container. If the order is important you will have to come up with something on your own.

    [edit]Beaten to the punch! [/edit]
    "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

  8. #8
    Registered User
    Join Date
    Dec 2004
    Posts
    50

    Index

    Hoe can I covert index of the biggest element in vector as a integer or other number or index for some other container?

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Can you rephrase that?
    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

  10. #10
    Registered User
    Join Date
    Dec 2004
    Posts
    50

    Rephrasal

    I used function max_number and I got the iterator.
    I want to use this iterator for some calculation and also as an index of another vector?
    How can I do it?

  11. #11
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,802
    Check your other post, I may have mislead you a bit... the max_element function returns an iterator (pointer) and not a value you can use (directly) as an array index. I apologize for any confusion. You can use the value returned from max_element along with the distance function to get a value you can use as an index. I posted an example of that in the other post.

    [edit]Ha Ha beat you CornedBee! [/edit]
    "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
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Use std::distance from <iterator> to calculate the distance between this iterator and the begin() iterator.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. syntax help?
    By scoobygoo in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2007, 10:38 AM
  3. Randomly rearranging the elements in a vector
    By Signifier in forum C++ Programming
    Replies: 11
    Last Post: 08-01-2007, 12:21 PM
  4. deleting selected elements in a vector
    By earth_angel in forum C++ Programming
    Replies: 14
    Last Post: 07-15-2005, 11:38 PM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21