Thread: Determine array size

  1. #16
    Registered User trainee's Avatar
    Join Date
    Jan 2004
    Originally posted by eth0
    Thanks for the tip with the vectors. I will research that method today. Probably be back with questions though
    However, does the vector route not require that you keep a running total of how many elements are in the array?

    jlou, I am already going down the route you suggested.

    I am very determined to learn this lanuage, however it is very difficult to learn with books alone.

    I am probably spending around 8 hours a day now trying to learn this. If I worked at a company with programmers, I know I could learn this in a quater of the time, as an experienced programmer to refer to when your stuck, can mean the difference between 10 minutes and 1 week. Even then at the end of the week your still uncertain yu tackled something in the best way.

    I will deffinitely achieve this, i'm just trying to stress how difficult it is when you are on your own. People post code and solutions on here without explaining, which just goes totaly over your head. All part of the fun though
    Vectors are a powerful array abstraction. Use the size() member function to return the number of values in the array at any time. The push_back(string) member function also dynamically appends a value to the end of your vector.

    Example usage:
    #include <iostream>
    #include <string>
    #include <vector>
    using std::cout;
    using std::endl;
    using std::string;
    using std::vector;
    int main(void)
        vector<string> vec; // here 'string' can be replaced with any object or simple data type
        vec.push_back("Joe"); // corresponding data type
        cout << vec.size();
        return 0;
    Vectors overload many of the standard library operators as well so they should behave like a standard array. For example, they still use [i] style indexing if you want to access a single member. Fool around with it and if you need more help, post.

    Last edited by trainee; 01-07-2004 at 02:52 PM.

  2. #17
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    I just started my STL learning too and whilst I agree that vectors are the best thing for this sort of problem, I do believe you're being taught how to solve a problem at the most basic level. So instead of using vectors try working around your problems with the particular code implementation. The easiest solution I can offer to your problem is to create a static variable which you decrement and increment as you remove and insert from the array that way you just have to query the variable for the number of elements that are in the array.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to determine length of pointer array
    By Dr.Zoidburg in forum C Programming
    Replies: 12
    Last Post: 02-16-2009, 06:52 PM
  2. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  3. Determine length of an array
    By ulillillia in forum C Programming
    Replies: 7
    Last Post: 04-21-2007, 08:32 PM
  4. An exercise in optimization
    By Prelude in forum Contests Board
    Replies: 10
    Last Post: 04-29-2005, 03:06 PM