LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-04-2005, 12:23 AM   #1
bendeco13
Member
 
Registered: Oct 2004
Distribution: Fedora 7
Posts: 232

Rep: Reputation: 30
c++ vector


This may be a bit of a dumb question, considering I don't think there is any way to do what I'm wanting, but I guess thats what questions are for.

I have a program and I want to define a list of strings. I can do this as an array, but then I can't determine the size of the array at a later time. So I decided I would have to use vectors.

I tried this, but it surely didn't work.
Code:
vector<string> path_column_names = {"Name", "Last Changed"};
vector<string> file_column_names = {"Name", "Size", "Last Changed"};
I know I could define the two and then use push_back(), but I don't want to do this, because I want others to be able to easily (see) and modify this at a later time.
Is there some other way of doing this without using the push_back() method?

Thanks in advance,
Bendeco
 
Old 11-04-2005, 09:34 AM   #2
YetAnotherDave
Member
 
Registered: Feb 2005
Posts: 95

Rep: Reputation: 17
How about something like this? It's not exactly what you want but perhaps better than push_back() .

Code:
#define SPAN(Z) (Z), ((Z)+sizeof(Z)/sizeof(Z[0]))

char* pc[] = {"Name", "Last Changed"};
vector<string> path_column_names(SPAN(pc));

char* fc[] = {"Name", "Size", "Last Changed"};
vector<string> file_column_names(SPAN(fc));
- Dave
 
Old 11-04-2005, 12:42 PM   #3
bendeco13
Member
 
Registered: Oct 2004
Distribution: Fedora 7
Posts: 232

Original Poster
Rep: Reputation: 30
Smile

That does exactly what I wanted.
Thanks alot ...

If you don't mind me asking, I'm a little new to C++, can you describe what the
#define SPAN(Z) (Z), ((Z)+sizeof(Z)/sizeof(Z[0]))
actually does/means?

I know what the preprocessor directive define does, I'm just wandering about everything else.
Thanks

Last edited by bendeco13; 11-04-2005 at 12:45 PM.
 
Old 11-04-2005, 01:32 PM   #4
YetAnotherDave
Member
 
Registered: Feb 2005
Posts: 95

Rep: Reputation: 17
The expression "sizeof(Z)/sizeof(Z[0])" in SPAN(Z) is the number of elements in the Z array.

The "(Z)" part of SPAN(Z) is a pointer to the first entry in the Z array and the "((Z)+sizeof(Z)/sizeof(Z[0]))" part points one element past the last entry in the array.

So far this looks complicated but I think it will work even in standard C ( I didn't try it though ).

A pointer will work as an stl iterator so constructing a vector with the two pointers result in the following vector constructor being called:

Code:
template <class InputIterator> 
vector(InputIterator, InputIterator)

which results in a vector being created from the objects that the iterators/pointers from the first argument ( inclusive ) to the last argument ( non-inclusive ) point to.



Here's the same thing without the use of the macro and with an intermediate variable which might make things clearer:

Code:
char* pc[] = {"Name", "Last Changed"};
unsigned int pcLen = sizeof(pc)/sizeof(pc[0]);  // this evaluates to 2
vector<string> path_column_names(pc, pc+pcLen);

I didn't compile the code in this response so there may be typos.

- Dave
 
Old 11-04-2005, 02:22 PM   #5
bendeco13
Member
 
Registered: Oct 2004
Distribution: Fedora 7
Posts: 232

Original Poster
Rep: Reputation: 30
Thanks again...
That pretty much clears it all up.
It's people like you who keep us inspired to program.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
vector like x ui oldstinkyfish Linux - Software 1 07-20-2005 11:36 AM
can i modify int 80 vector to a user-defined vector qqrilxk Linux - Security 1 03-03-2005 08:46 PM
Merge Two Vector values into new vector ArulPrabhuT Programming 2 09-23-2004 10:18 AM
Miseries at install from HDD&floppies, with vector ISO& how to boot with vector linux prctom VectorLinux 9 06-29-2004 05:27 AM
what's going on with Vector?? yowwww Linux - General 15 06-12-2003 09:47 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration