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

1. 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:

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

- DARKGuy

2. 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 {
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.

3. Originally Posted by Blackroot
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. 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. 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. Provide a small, simple, but complete program that demonstrates this problem.

7. 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.