C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-09-2007, 11:54 PM   #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
DARKGuy is offline   Reply With Quote
Old 09-10-2007, 12:12 AM   #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.
Blackroot is offline   Reply With Quote
Old 09-10-2007, 01:19 AM   #3
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,762
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.
brewbuck is offline   Reply With Quote
Old 09-10-2007, 02:11 AM   #4
Super Moderator
 
Bubba's Avatar
 
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   Reply With Quote
Old 09-10-2007, 08:56 AM   #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.
DARKGuy is offline   Reply With Quote
Old 09-10-2007, 09:05 AM   #6
C++ Witch
 
laserlight's Avatar
 
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   Reply With Quote
Old 09-10-2007, 03:41 PM   #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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 08:18 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22