Thread: classes and members

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I'll google vectors to see what i can come up with, i dont know about vectors, maybe im in too deep
    You can use vectors just like arrays--except they have no fixed size. The basics are very easy to learn. Here's some of the basics using ints as the elements of the vector:
    Code:
    #include <iostream>
    #include <vector> /***/
    
    using namespace std;
    
    int main()
    {
    	vector<int> numbers(3); //declare a vector of ints with size 3
    	numbers[0] = 10;
    	numbers[1] = 20;
    	
    	cout<<numbers.size()<<endl; //3
    	
            numbers[2] = 30;
    
    	for(int i = 0; i<3; i++)
    	{
    		cout<<numbers[i]<<endl;
    	}
    	
    	numbers.push_back(40); //used to expand the size of the vector
    	cout<<numbers[3]<<endl;
    
    	cout<<numbers.size()<<endl; //4
    	cout<<numbers.capacity()<<endl; //6
    
    	return 0;
    }
    push_back() :
    Adds the argument to the end of the vector, expands the vector if necessary.

    size() :
    If you declare the size of a vector, size() will return that number--even if there are fewer elements. Otherwise, size() will return the actual number of elements in the vector.

    capacity():
    Internally, it is inefficient to expand a vector's size by just 1 every time, so a vector automatically expands its size to something bigger. capacity() gives you the room available before the vector will have to be resized again.
    I don't think you'll need anything more than that to write your program.
    Last edited by 7stud; 08-04-2005 at 10:47 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. templated members in non-template classes
    By drrngrvy in forum C++ Programming
    Replies: 3
    Last Post: 04-07-2007, 01:38 PM
  2. alignment (classes, structs, members)
    By Raven Arkadon in forum C++ Programming
    Replies: 5
    Last Post: 04-07-2006, 06:51 AM
  3. Class with members as classes
    By justdoit22 in forum C++ Programming
    Replies: 8
    Last Post: 12-29-2003, 06:58 PM
  4. inheritance from classes own members?
    By atapi103 in forum C++ Programming
    Replies: 2
    Last Post: 09-02-2003, 10:19 PM
  5. Help with classes & strings members
    By GrNxxDaY in forum C++ Programming
    Replies: 7
    Last Post: 07-21-2002, 08:29 PM