Thread: Trying to store lots of values in a vector?

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    22

    Trying to store lots of values in a vector?

    Hey guys

    I need to store lots of values in a dynamic-sized array, so I opted for vectors. I've had some problems in the past trying to do something similar so I'm asking for someone to please give me a code sample of something basic of what I want to archieve. I need to store the following values:

    string username;
    int hour;
    int min;
    int sec;
    string lastLine;
    bool caps;
    bool chars;

    And maybe some more. I don't have the code I've been using right now (that's why I'm asking to please make me some code sample I can try later) but if I can recall correctly, I had problems with getting the vector size... if I tried to get it in main() where I created a new record (vector.push_back() ), vector.size() returned the right value (1), but if I tried to get it from another function, vector.size() would return 6!... it's confusing and it's been driving me nuts this week :/...

    If anybody could lend me a hand here I'd be really glad. I'll try to post the code I'm using in a few hours when I wake up and go to work :P

    Thanks in advance

    - DARKGuy

  2. #2
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Sounds like you want a doubly linked list,

    (Singly list tutorial, doubly linked is exactly the same, except you save a previous structure aswell)
    http://www.cprogramming.com/tutorial/lesson15.html

    Code:
    struct Something {
     string username;
     int hour;
     int min;
     int sec;
     string lastLine;
     bool caps;
     bool chars; 
     Something *prev;
     Something *next;
    };
    You just need to link the sections of the list together. You may not even need a doubly linked list depending on what you're using this for. If you really need a vector though, just throw everything into a struct and use the struct as your vector type.
    Last edited by Blackroot; 09-10-2007 at 12:15 AM.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Blackroot View Post
    You just need to link the sections of the list together. You may not even need a doubly linked list depending on what you're using this for. If you really need a vector though, just throw everything into a struct and use the struct as your vector type.
    I don't see how a hand-coded linked list is in any way preferable to a std::list, or even a std::vector, depending on the application.

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    std::vector::size returns the number of items in the vector. Note that the item at index 0 in the vector counts as 1 item.

    A hand coded doubly linked list is not going to benefit you much. There are plenty of STL container types designed to do just what you need.

    Each container has its pros and cons and each was designed to perform a certain task. Make sure you understand the container and its purpose prior to using it. For what you want to do a vector seems ok - if you do not need to insert items in the vector. If you need to do a lot of inserting then a vector is not the best container for the job.

    Check your compiler help file as it will tell you all about the different container types.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    22
    Well, I managed to get a hold of the code I've been using on my laptop, here's a snippet (it's way too damn long):

    Code:
    struct usrC
    {
    	string name;
    	int byCaps;
    	int byFlood;
    	int byLength;
    	string lastmsg;
    	int lasth;
    	int lastm;
    	int lasts;
    };
    
    std::vector <usrC> usuario;
    Inside main I do:

    Code:
    usuario.push_back(usrC());
    	cout << "Size: " << usuario.size() << endl;
    	usuario[0].name="jajaa";	
    	cout << "Name: " << usuario[0].name << endl;
    This works ok, it returns 1 as expected and should, but if I do the same thing in a function:

    Code:
    int findUser(){
    	cout << "Size 2: " << usuario.size() << endl;
    	return -1; // random return for this example's sake
    }
    It returns 6... why? there's only one occurrence of push_back for the vector in the whole code... I've tried to use structs and classes with no avail :/...

    By the way Bubba, it's actually a chat bot for controlling a game chat channel, similar to an IRC bot, but it keeps track of the users, their sanctions and such.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,402
    Provide a small, simple, but complete program that demonstrates this problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Try printing out the other 5 elements in the vector and see if that gives you an idea of where the extra data is coming from.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting vector via recursive functions
    By porsche911nfs in forum C++ Programming
    Replies: 18
    Last Post: 05-04-2009, 06:54 AM
  2. Extract values and store in new variable
    By cosmiccomputing in forum C Programming
    Replies: 3
    Last Post: 06-01-2008, 01:45 PM
  3. Problems defining classes
    By esmeco in forum C++ Programming
    Replies: 47
    Last Post: 10-24-2007, 01:13 PM
  4. how do i read 2 consecutive values in vector and compute?
    By dalearyous in forum C++ Programming
    Replies: 8
    Last Post: 03-30-2006, 04:22 PM
  5. Operators for 3D Vector Mathematics
    By Anarchist in forum C++ Programming
    Replies: 10
    Last Post: 01-31-2003, 07:33 PM