I am trying to create a 2D array of CStrings, however it needs to be declared
dynamically for my application. I have read up on numerous forums but I just
cant seem to find what I am looking for.
From what I have read the best way to do this would be to use a vector of
CStrings. However, I am not to sure how to declare this dynamocally (I am able
to do it by specifying the row and column size).
What I would like to be able to do is adress any CString in the following way:
myarray = "hello";
Please could someone give me an example of how to do it without having to
specify the row and column size?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I assume you mean "C string" rather than "CString" which is the name of the
string class in MFC?
The whole point about using a std::vector is that it handles the dynamic
memory allocation for you. However if you are going to go to do that, it would
make sense also to use std::string rather than C strings. In C++ a vector of
vectors of std::strings is the most flexible solution, and for large sparsely
populated arrays, the most memory efficient; but this is not truly an array so
may behave in ways that are not suited to your application.
There are a wide range of possible solutions, so we would need more
information in order to make the most appropriate recommendation.
Which parts are variable (and therefore dynamic); the number of rows, number
of columns, or length of strings, or any combination of these dimensions?
Are you using C++ rather than C? (since the best solution will differ).
If using C++, do you truly need C strings, or can you use std::string objects?
Do you truly need array semantics? Specifically must all memory allocated be
contiguous, or does that not matter? The solution can be different if you
don't need this.
Clifford.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Memory management is done automatically here. This is EXACTLY what I want,
however the poster claims it to be dynamic and I can't see that because the
"width" and "height" must be set here before compilation.
Any help or advice at all will be greatly, greaty appreciated! I have been
struggling with this now for 2days....
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
First off, I apologise for not being more specific. To be more exact I am
speaking with regard to the "CString" class in MFC.
Then you are possibly in the wrong forum. That is not to say that there are
not people here who know the answer, but rather that this forum is
specifically related to Dev-C++, and Dev-C++ does not support MFC. More
general forums include and .
the poster claims it to be dynamic and I can't see that because the "width"
and "height" must be set here before compilation.
Not at all, you can change the size of a dynamically using push_back(),
insert() and erase() members, or passed to the constructor. The values passed
to the constructor need not be compile-time constants unless the vector itself
is statically allocated.
Thanks again for your help. I have got it working now!
Then you are possibly in the wrong
forum. That is not to say that there
are not people here who know the
answer, but rather that this forum is
specifically related to Dev-C++, and
Dev-C++ does not support MFC. More
general forums include Stack Overflow
and Dev Shed.
Noted, sorry about that.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi All,
I am trying to create a 2D array of CStrings, however it needs to be declared
dynamically for my application. I have read up on numerous forums but I just
cant seem to find what I am looking for.
From what I have read the best way to do this would be to use a vector of
CStrings. However, I am not to sure how to declare this dynamocally (I am able
to do it by specifying the row and column size).
What I would like to be able to do is adress any CString in the following way:
myarray = "hello";
Please could someone give me an example of how to do it without having to
specify the row and column size?
I assume you mean "C string" rather than "CString" which is the name of the
string class in MFC?
The whole point about using a std::vector is that it handles the dynamic
memory allocation for you. However if you are going to go to do that, it would
make sense also to use std::string rather than C strings. In C++ a vector of
vectors of std::strings is the most flexible solution, and for large sparsely
populated arrays, the most memory efficient; but this is not truly an array so
may behave in ways that are not suited to your application.
There are a wide range of possible solutions, so we would need more
information in order to make the most appropriate recommendation.
Which parts are variable (and therefore dynamic); the number of rows, number
of columns, or length of strings, or any combination of these dimensions?
Are you using C++ rather than C? (since the best solution will differ).
If using C++, do you truly need C strings, or can you use std::string objects?
Do you truly need array semantics? Specifically must all memory allocated be
contiguous, or does that not matter? The solution can be different if you
don't need this.
Clifford.
Hi thanks very much for taking the time to reply.
First off, I apologise for not being more specific. To be more exact I am
speaking with regard to the "CString" class in MFC.
The parts which are variable and therefore dynamic are:
The number of colums must be set to 3.
Therefore, for example:
I have a client server application which connect via the OPC interface and
make use of the callback function.
Everytime process data changes on the client, the server must save the data in
an array, hopefully as follows:
MyArray="Message ID in CString format"
MyArray="Message in CString format"
MyArray="Time in CString format"
MyArray="Message ID in CString format"
MyArray="Message in CString format"
MyArray="Time in CString format"
and so on....the data is then compared for various reasons and finally written
to an SQL Database...
The entire application has been written in C++ so far.
I am just really struggling with this array structure.
I found the following code on another forum:
include <vector>
std::vector<std::vector<CString> > your2darray(width,
std::vector<CString>(height));
your2darray = "asdf";
Memory management is done automatically here. This is EXACTLY what I want,
however the poster claims it to be dynamic and I can't see that because the
"width" and "height" must be set here before compilation.
Any help or advice at all will be greatly, greaty appreciated! I have been
struggling with this now for 2days....
Then you are possibly in the wrong forum. That is not to say that there are
not people here who know the answer, but rather that this forum is
specifically related to Dev-C++, and Dev-C++ does not support MFC. More
general forums include and .
Not at all, you can change the size of a dynamically using push_back(),
insert() and erase() members, or passed to the constructor. The values passed
to the constructor need not be compile-time constants unless the vector itself
is statically allocated.
: http://stackoverflow.com/
: http://forums.devshed.com/c-programming-42/
: http://www.cplusplus.com/reference/stl/vector/
Thanks again for your help. I have got it working now!
Noted, sorry about that.