473,383 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Is there a class for a dynamicly resizable array?

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
9 1721
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
"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
> 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
"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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: David. E. Goble | last post by:
Hi all; I need to build a list of strings in one function and use the list in another function. ie buildlist(char list, FILE **infile); { int i;
4
by: Bill Sun | last post by:
Hi, All I have a conventional question, How to create a 3 dimension array by C language. I see some declare like this: int *** array; int value; array = create3Darray(m,n,l);
1
by: Henke | last post by:
Hi I have a aspx-page with a panel-control. On this panel control I add user controls dynamicly with LoadControl and panel.Controls.Add(myControl). On some of the dynamicly added user controls I...
4
by: sam.s.kong | last post by:
Hello! I opened a new window using 'resizable=no'. Can the child window make itself resizable even if it's opened with 'resizable=no'? For example: <body onload="javascript:...
1
by: Rako | last post by:
My problem is: I want to create an index to any of the available picture-groups. This index is a table of thumbs with a scrollbar. If you click on the thumb, you get the full picture displayed. ...
5
by: AlexVN | last post by:
Hi, I would like to know if someone investigated the method of creating classes dynamically depending on the name. I do not like a lot of ifs and I suppose that something like...
3
by: Al Henderson | last post by:
Morning All, I have a web application where we pop up a little calendar control in a new window to allow users to choose dates. For cross-browser purposes, this is done via window.open (with...
3
by: Thierry Loiseau | last post by:
Hello, If I do : w=window.open('','a','resizable=no, width=400, height=400, toto=3'); can I modify 'w' to resizable=yes ? Thank you, Thierry
5
AdrianH
by: AdrianH | last post by:
Hi, I know that there are a bunch of window class names used in the Win32 API (I think "BUTTON" is one). Does anyone know a good web reference for all of these so I can create them dynamicly using...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.