Thread: intializing vector

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

    intializing vector

    Code:
    # include<vector>
    ....
    main()
     {
       vector <int> k;
        k[0]=1;
        k[1]=2;
     }
    I receive failure notice during compiling. Do I have to use pushback command or I can avoid it?

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    The vector name is a handle and only contains enough memory to allow the vector to manage the storage of it's elements. A vector internally allocates storage on the free store for the space to store the elements. So, if you don't tell a vector initially how many elements to create storage for, you have to use the vector class methods, like push_back() to create storage for a new element.

    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    int main()
    {
        int a[10];
        cout<<sizeof a<<endl; //int's are 4 bytes each, so size=40
    
        vector <int> v(10);  
        cout<<sizeof v<<endl;  //size is only 16
    
        v[0]=1;
        v[1]=2;
        
        cout<<v[0]<<endl<<v[1]<<endl;
    
        vector <int> k;  //doesn't create any memory for the elements
        k.push_back(10);
        k.push_back(20);
    
        cout<<k[0]<<endl<<k[1]<<endl;
        
    
        return 0;
    }
    Last edited by 7stud; 02-07-2005 at 12:01 PM.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    What 7stud says is correct, but it can't be the cause for your error, because that stuff is not detected until runtime.

    What is the error message you get? I see that you forgot to give main a return type.
    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

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

    Vector size

    Does it mean that size of a vector with one element is 0?

  5. #5
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by strickey
    I receive failure notice during compiling.
    Maybe you forgot a using directive?
    Code:
    #include<vector>
    using std::vector;
    
    int main()
    {
       vector <int> k(2);
       k[0]=1;
       k[1]=2;
       return 0;
    }
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by strickey
    Does it mean that size of a vector with one element is 0?
    The size (as returned by the size member function) will be 1. Internally however, the size of a vector object (as returned by sizeof) is fixed. The vector object maintains what are essentially some pointers to the data area (which grows as needed), along with a couple extra data variables that it needs:

    Code:
    vector<int> v;
    
    cout << sizeof(v) << ',' << v.size() << endl;
    
    v.push_back(10);
    cout << sizeof(v) << ',' << v.size() << endl;
    
    v.push_back(20);
    cout << sizeof(v) << ',' << v.size() << endl;
    Output on my machine:
    Code:
    16,0
    16,1
    16,2
    "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. 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. Vector class
    By Desolation in forum C++ Programming
    Replies: 2
    Last Post: 05-12-2007, 05:44 PM
  4. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 12:34 PM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM