CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2006
    Location
    Germ many
    Posts
    1

    Talking vector understanding

    This is abou vector
    PHP Code:
    #include<vector>

    void vect(std::vector<int>bint i){
        
    b.remove(i);
    }

    void main(){
        
    std::vector<inth
        
    vect(h,1);

        
    double x=0y=0    ;
        
    h.push_back(x);
        
    h.push_back(y);
        return 
    0;

    But it gave me a lot of error I don't understand . This is part of a a smell project at school.
    If u give more more advice, i promise you i will be back to say more thanks.

    Thanks for your inputs, you are helpfull, very full.
    Douglas999

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: vector understanding

    1) You need to pass the vector by reference, not by value.

    2) I assume you are gtrying to remove the i-th element from the
    vector. The member function is called erase(), not remove(). Also
    it takes an iterator, not an index.

    Code:
    void vect(std::vector<int> & b, int i){ 
    //assumes that there are at least (i+1) elements in the vector
        b.erase(b.begin()+i); 
    }

  3. #3
    Join Date
    Aug 2004
    Posts
    148

    Re: vector understanding

    Code:
    std::vector<int> h;
        vect(h,1);
    
        double x=0, y=0    ;
        h.push_back(x);
        h.push_back(y);
    You are also trying to push doubles onto a int vector..

  4. #4
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: vector understanding

    void main(){
    The correct function prototype is int main.
    std::vector<int> h;
    vect(h,1);
    What is it supposed to do?
    Remove the second (vector begin at 0) element of an empty vector.

    Perhaps, you should move that operation after the second push_back method call.
    Code:
    #include <vector>
    
    void vect(std::vector<int> b, std::vector<int>::size_type i){ // std::vector<int>::size_type is an unsigned integer whose size is adequate for vector indexing
       b.erase(b.begin()+i);
    }
    
    int main(){
        std::vector<int> h;
    
        int x=0, y=0    ;
        h.push_back(x);
        h.push_back(y);
        vect(h,1); // this operation and the previous mutually anihilate themselves.
        return 0;
    }
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured