By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,852 Members | 2,219 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,852 IT Pros & Developers. It's quick & easy.

Is there a class for a dynamicly resizable array?

P: n/a
Basicly, I need to store a string plus a data structure for each entry in
this array. It needs to be able to get bigger (but I wont be deleting from
it). Also, I need to be able to store the elements in this array somehow.

Jul 22 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
In article <3f********@dnews.tpgi.com.au>,
Jonathan Wilson <jo****@tpgi.com.au> wrote:
Basicly, I need to store a string plus a data structure for each entry in
this array. It needs to be able to get bigger (but I wont be deleting from
it). Also, I need to be able to store the elements in this array somehow.


The 'vector' class in the C++ standard library should fill your needs.
Here's a simple example that uses a vector of strings. You can
generalize it by defining a class or struct to hold whatever you want, and
making a vector of that.

#include <vector>
#include <string>
#include <iostream>

using namespace std;

int main ()
{
vector<string> foo2; // start with a zero-length vector
foo2.push_back("Hello,"); // vector expands automatically
foo2.push_back("my");
foo2.push_back("name");
foo2.push_back("is");
foo2.push_back("Munich.");

// find out how big the vector is by using the size() member function.

for (int k = 0; k < foo2.size(); ++k)
cout << foo2[k] << " ";
cout << endl;

return 0;
}

--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 22 '05 #2

P: n/a
"Jonathan Wilson" <jo****@tpgi.com.au> wrote...
Basicly, I need to store a string plus a data structure for each entry in
this array. It needs to be able to get bigger (but I wont be deleting from
it). Also, I need to be able to store the elements in this array somehow.


vector<pair<string,yourdatastructure> >

I am not sure what you mean by "I need to be able to store the elements
in this array somehow".

Victor
Jul 22 '05 #3

P: n/a
> I am not sure what you mean by "I need to be able to store the elements
in this array somehow".

I meant sort not store.
Can one easliy sort the elements in a vector somehow?

Jul 22 '05 #4

P: n/a
"Jonathan Wilson" <jo****@tpgi.com.au> wrote:
I am not sure what you mean by "I need to be able to store the elements
in this array somehow".

I meant sort not store.
Can one easliy sort the elements in a vector somehow?


std::vector vec;
.... fill the vector with values ...
std::sort(vec.begin(), vec.end());

David F
Jul 22 '05 #5

P: n/a
In article <Tt******************@nasal.pacific.net.au>,
David Fisher <no****@nospam.nospam.nospam> wrote:
"Jonathan Wilson" <jo****@tpgi.com.au> wrote:

Can one easliy sort the elements in a vector somehow?


std::vector vec;
... fill the vector with values ...
std::sort(vec.begin(), vec.end());


If the vector is filled with a user-defined data type, you need to define
operator<() for that data type, in order to do the comparisons, or else
pass a comparison function as a third argument to std::sort().

--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 22 '05 #6

P: n/a
Jonathan Wilson <jo****@tpgi.com.au> wrote in message news:<3f********@dnews.tpgi.com.au>...
Basicly, I need to store a string plus a data structure for each entry in
this array. It needs to be able to get bigger (but I wont be deleting from
it). Also, I need to be able to store the elements in this array somehow.


You could use the standard collections: a std::vector of std::pair
Ex:
typedef std::pair<string, my_structure> MyPair;
std::vector<MyPair> vect;

, or a std::map<string, my_structure>
Jul 22 '05 #7

P: n/a
On Thu, 04 Dec 2003 13:04:29 +0800, Jonathan Wilson
<jo****@tpgi.com.au> wrote:
I am not sure what you mean by "I need to be able to store the elements
in this array somehow".

I meant sort not store.
Can one easliy sort the elements in a vector somehow?


Depending on how you use your "array", you might want to use
std::vector<my_pair> or std::list<my_pair>:

When you sort a vector, you are actually moving its elements around,
which could be slow if your vector is large. Also, if you have
pointers to elements of the vector, or iterators, and then sort it,
your pointers and iterators become invalid.

The list container, on the other hand, is not contiguous: Each element
could be anywhere in memory, and each element is chained to previous
and next via pointers. It is usually faster to sort, add and delete.

With std::list nothing is moved, as sorting, adding and deleting only
change pointers to previous and next. To use it you need to,

#include <list>

And to make sure that the STL is in your path.

Cheers!
Jul 22 '05 #8

P: n/a
Jonathan Wilson wrote:

Basicly, I need to store a string plus a data structure for each entry in
this array. It needs to be able to get bigger (but I wont be deleting from
it). Also, I need to be able to store the elements in this array somehow.


After lots of discussion about std::vector:

Depending on your exact neees a simple std::map or a std::multimap
could be simpler.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #9

P: n/a
jb
Jonathan Wilson <jo****@tpgi.com.au> wrote in message news:<3f********@dnews.tpgi.com.au>...
Basicly, I need to store a string plus a data structure for each entry in
this array. It needs to be able to get bigger (but I wont be deleting from
it). Also, I need to be able to store the elements in this array somehow.


vector...perhaps???
Jul 22 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.