Is there a class for a dynamicly resizable array?

  • Thread starter Jonathan Wilson
  • Start date
J

Jonathan Wilson

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

Jon Bell

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;
}
 
V

Victor Bazarov

Jonathan Wilson said:
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
 
J

Jonathan Wilson

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?
 
D

David Fisher

Jonathan Wilson said:
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
 
J

Jon Bell

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().
 
F

Flaviu Matan

Jonathan Wilson said:
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>
 
D

Dan W.

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!
 
K

Karl Heinz Buchegger

Jonathan said:
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.
 
J

jb

Jonathan Wilson said:
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???
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top