![]() |
| | #1 |
| Registered User Join Date: May 2006
Posts: 22
| Trying to store lots of values in a vector? ![]() 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 |
| DARKGuy is offline | |
| | #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;
};
Last edited by Blackroot; 09-10-2007 at 12:15 AM. |
| Blackroot is offline | |
| | #3 |
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,762
| 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. |
| brewbuck is offline | |
| | #4 |
| Super Moderator Join Date: Aug 2001
Posts: 7,813
| 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. |
| Bubba is offline | |
| | #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;
Code: usuario.push_back(usrC()); cout << "Size: " << usuario.size() << endl; usuario[0].name="jajaa"; cout << "Name: " << usuario[0].name << endl; Code: int findUser(){
cout << "Size 2: " << usuario.size() << endl;
return -1; // random return for this example's sake
}
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. |
| DARKGuy is offline | |
| | #6 |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,322
| Provide a small, simple, but complete program that demonstrates this problem.
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way |
| laserlight is online now | |
| | #7 |
| and the hat of sweating Join Date: Aug 2007 Location: Toronto, ON
Posts: 3,279
| 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. |
| cpjust is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Sorting vector via recursive functions | porsche911nfs | C++ Programming | 18 | 05-04-2009 06:54 AM |
| Extract values and store in new variable | cosmiccomputing | C Programming | 3 | 06-01-2008 01:45 PM |
| Problems defining classes | esmeco | C++ Programming | 47 | 10-24-2007 01:13 PM |
| how do i read 2 consecutive values in vector and compute? | dalearyous | C++ Programming | 8 | 03-30-2006 04:22 PM |
| Operators for 3D Vector Mathematics | Anarchist | C++ Programming | 10 | 01-31-2003 07:33 PM |