473,326 Members | 2,023 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,326 software developers and data experts.

vector of char arrays

I have a fixed-size string array like this

char m_titles[80000][8];

and can't figure out how to change it to a dynamically-sized vector like
this:

std::vector<char[8]> m_titles;

The declaration compiles but then calling reserve or resize leads to
obscure compiler errors. Can this be done so that it allocates one big
array of chars, not 80000 heap strings?

--
Scott McPhillips [VC++ MVP]

Jul 22 '05 #1
10 2386
Scott McPhillips [MVP] wrote:
I have a fixed-size string array like this

char m_titles[80000][8];

and can't figure out how to change it to a dynamically-sized vector
like this:

std::vector<char[8]> m_titles;

The declaration compiles but then calling reserve or resize leads to
obscure compiler errors.
Yeah, this fails both copyconstuctibility and assignability.
Can this be done so that it allocates one
big array of chars, not 80000 heap strings?


You can use a struct with a member array, but you can't be sure there won't be
padding between the instances.

Jonathan
Jul 22 '05 #2
Scott McPhillips [MVP] wrote:
I have a fixed-size string array like this

char m_titles[80000][8];

and can't figure out how to change it to a dynamically-sized vector like
this:

std::vector<char[8]> m_titles;

The declaration compiles but then calling reserve or resize leads to
obscure compiler errors. Can this be done so that it allocates one big
array of chars, not 80000 heap strings?

The problem is that the type char[8] isn't assignable so it's not going
to work inside a vector.

You could either do a vector of vector,
or a vector of strings,
or
struct c8 {
char c[8];
};
vector<c8>

Jul 22 '05 #3

"Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp> wrote in message
news:8o********************@comcast.com...
I have a fixed-size string array like this

char m_titles[80000][8];

and can't figure out how to change it to a dynamically-sized vector like
this:

std::vector<char[8]> m_titles;

The declaration compiles but then calling reserve or resize leads to
obscure compiler errors. Can this be done so that it allocates one big
array of chars, not 80000 heap strings?


Standard containers require that the contained objects
be copyable and assignable. Arrays do not qualify.

Use a vector of vectors.

-Mike
Jul 22 '05 #4
Scott McPhillips [MVP] wrote:
I have a fixed-size string array like this

char m_titles[80000][8];

and can't figure out how to change it to a dynamically-sized vector like this:

std::vector<char[8]> m_titles;


Others have commented on this issue already. I just want to point out
that the upcoming library TR has an elegant solution to this which is
not as expensive as a vector of vectors: 'std::tr1::array' is a class
template for fixed size arrays. The replacement would look like this:

| std::vector<std::tr1::array<char, 8> > m_titles;

Unfortunately, TR1 implementations are not yet available, AFAIK. On the
other hand, Boost has an implementation of 'array' which is close if
not
identical to the TR1 version (see <http://www.boost.org/>).
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 22 '05 #5

"Dietmar Kuehl" <di***********@yahoo.com> wrote in message > Others have
commented on this issue already. I just want to point out
that the upcoming library TR has an elegant solution to this which is
not as expensive as a vector of vectors: 'std::tr1::array' is a class
template for fixed size arrays. The replacement would look like this:

| std::vector<std::tr1::array<char, 8> > m_titles;

Unfortunately, TR1 implementations are not yet available, AFAIK. On the
other hand, Boost has an implementation of 'array' which is close if
not
identical to the TR1 version (see <http://www.boost.org/>).
--


Just curious: what's the "tr1" stand for? Is it really going to be "tr1"?
Having to use std:: isn't bad, but having to do std::tr1: is starting to
look like crap to me. I know it's nice to limit the number of characters in
an identifier (to reduce typing), but at least I try to make my id's
somewhat meaningful. I just hate to see my c++ code cluttered with names
that make no earthly sense! :-)
Thanks,
-Howard

Jul 22 '05 #6
Howard wrote:
Just curious: what's the "tr1" stand for? Is it really going to be "tr1"?
Having to use std:: isn't bad, but having to do std::tr1: is starting to
look like crap to me. I know it's nice to limit the number of characters in
an identifier (to reduce typing), but at least I try to make my id's
somewhat meaningful. I just hate to see my c++ code cluttered with names
that make no earthly sense! :-)

This is some text from my site:

There will be additional ISO C++ library facilities in a library named
TR1 and in namespace std::tr1, due to 2005-2006 time frame. Proposals
that have been accepted are here:

http://www.open-std.org/jtc1/sc22/wg...al_report.html


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #7
Howard wrote:
Just curious: what's the "tr1" stand for?
"First (Library) Technical Report". Since "1tr" or "1thtr do not work
and
"firsttr" is much longer, we settled on "tr1". That it is technically
not
really the first technical report issued by the C++ committee (this was
the TR on performance) may be a little strange but it still is the
first
library TR.
Is it really going to be "tr1"?
See Section 1.3 (tr.intro.namespaces) of the current document
(<http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1711.pdf>).
Having to use std:: isn't bad, but having to do std::tr1: is starting to look like crap to me.
If the components make it into C++0x they will be moved to 'std::'.
However,
the TR does not really modify the standard and it is unclear whether
the
components will really become part of the standard and if so, whether
they
remain unchanged. Putting the names into a clearly distinguished
namespace
which is only used for that purpose allows implementers to retain the
TR
version for compatibility issues and still provide the standard version
in
the appropriate namespace.

It would have been possible to choose '::tr1' (i.e. not nest it into
namespace
'std') but this would possibly be in conflict with user namespaces:
'::tr1' is
definitely not reserved. 'std::tr1' is a safe place where a user shall
not
place his own names (if he does so, he deserves what he gets). Of
course,
'tr1' may be a macro defined by users. In this case, a user cannot use
the
'tr1' components but that's bad luck. Of course, loads of new names
which
cannot be used as macro names are introduced by the TR but since it is
conventional to at least start macro names with a capital letter, the
chances
of conflicts are actually pretty low.
I know it's nice to limit the number of characters in
an identifier (to reduce typing), but at least I try to make my id's
somewhat meaningful.
'std::tr1' *is* meaningful. It is clearly the best name available: even
dropping the '1' to make it shorter is not a reasonable choice as we
will
start working on 'tr2' ("Second (Library) Technical Report") at the
next
meeting in Lillehammer. We might get rid of the 'r', though, but I
prefer
'tr1' over 't1'.
I just hate to see my c++ code cluttered with names
that make no earthly sense! :-)


You are free to use using directives or using declarations and only
that
part of your code uses these names. I wouldn't call that cluttering. Of
course, you are also free not to use the TR1 components at all.
Personally, I don't think the latter approach is really such a smart
move
once implementations of TR1 are widely available.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 22 '05 #8

"Dietmar Kuehl" <di***********@yahoo.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
Howard wrote:
Just curious: what's the "tr1" stand for?
"First (Library) Technical Report". Since "1tr" or "1thtr do not work
and
"firsttr" is much longer, we settled on "tr1". That it is technically
not
really the first technical report issued by the C++ committee (this was
the TR on performance) may be a little strange but it still is the
first
library TR.
Is it really going to be "tr1"?


See Section 1.3 (tr.intro.namespaces) of the current document
(<http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1711.pdf>).
Having to use std:: isn't bad, but having to do std::tr1: is starting

to
look like crap to me.


If the components make it into C++0x they will be moved to 'std::'.
However,
the TR does not really modify the standard and it is unclear whether
the
components will really become part of the standard and if so, whether
they
remain unchanged. Putting the names into a clearly distinguished
namespace
which is only used for that purpose allows implementers to retain the
TR
version for compatibility issues and still provide the standard version
in
the appropriate namespace.

It would have been possible to choose '::tr1' (i.e. not nest it into
namespace
'std') but this would possibly be in conflict with user namespaces:
'::tr1' is
definitely not reserved. 'std::tr1' is a safe place where a user shall
not
place his own names (if he does so, he deserves what he gets). Of
course,
'tr1' may be a macro defined by users. In this case, a user cannot use
the
'tr1' components but that's bad luck. Of course, loads of new names
which
cannot be used as macro names are introduced by the TR but since it is
conventional to at least start macro names with a capital letter, the
chances
of conflicts are actually pretty low.
I know it's nice to limit the number of characters in
an identifier (to reduce typing), but at least I try to make my id's
somewhat meaningful.


'std::tr1' *is* meaningful.


Well, it's meaningful if you know what it means! :-)

(I was thinking along the lines of using a name like "Length" instead of "l"
in my code. And in that respect "tr1" by itself doesn't tell me much.
Something like "ext05" might tell me that it was the "2005 extension". But
no problem, I'm a big boy, and can deal with it! :-))
It is clearly the best name available: even
dropping the '1' to make it shorter is not a reasonable choice as we
will
start working on 'tr2' ("Second (Library) Technical Report") at the
next
meeting in Lillehammer. We might get rid of the 'r', though, but I
prefer
'tr1' over 't1'.
I just hate to see my c++ code cluttered with names
that make no earthly sense! :-)


You are free to use using directives or using declarations and only
that
part of your code uses these names. I wouldn't call that cluttering. Of
course, you are also free not to use the TR1 components at all.
Personally, I don't think the latter approach is really such a smart
move
once implementations of TR1 are widely available.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting


Okee dokee. Thanks for the info, guys!
-H

Jul 22 '05 #9
Howard wrote:
Just curious: what's the "tr1" stand for?
"First (Library) Technical Report". Since "1tr" or "1thtr do not work
and
"firsttr" is much longer, we settled on "tr1". That it is technically
not
really the first technical report issued by the C++ committee (this was
the TR on performance) may be a little strange but it still is the
first
library TR.
Is it really going to be "tr1"?
See Section 1.3 (tr.intro.namespaces) of the current document
(<http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1711.pdf>).
Having to use std:: isn't bad, but having to do std::tr1: is starting to look like crap to me.
If the components make it into C++0x they will be moved to 'std::'.
However,
the TR does not really modify the standard and it is unclear whether
the
components will really become part of the standard and if so, whether
they
remain unchanged. Putting the names into a clearly distinguished
namespace
which is only used for that purpose allows implementers to retain the
TR
version for compatibility issues and still provide the standard version
in
the appropriate namespace.

It would have been possible to choose '::tr1' (i.e. not nest it into
namespace
'std') but this would possibly be in conflict with user namespaces:
'::tr1' is
definitely not reserved. 'std::tr1' is a safe place where a user shall
not
place his own names (if he does so, he deserves what he gets). Of
course,
'tr1' may be a macro defined by users. In this case, a user cannot use
the
'tr1' components but that's bad luck. Of course, loads of new names
which
cannot be used as macro names are introduced by the TR but since it is
conventional to at least start macro names with a capital letter, the
chances
of conflicts are actually pretty low.
I know it's nice to limit the number of characters in
an identifier (to reduce typing), but at least I try to make my id's
somewhat meaningful.
'std::tr1' *is* meaningful. It is clearly the best name available: even
dropping the '1' to make it shorter is not a reasonable choice as we
will
start working on 'tr2' ("Second (Library) Technical Report") at the
next
meeting in Lillehammer. We might get rid of the 'r', though, but I
prefer
'tr1' over 't1'.
I just hate to see my c++ code cluttered with names
that make no earthly sense! :-)


You are free to use using directives or using declarations and only
that
part of your code uses these names. I wouldn't call that cluttering. Of
course, you are also free not to use the TR1 components at all.
Personally, I don't think the latter approach is really such a smart
move
once implementations of TR1 are widely available.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 22 '05 #10
Howard wrote:
Well, it's meaningful if you know what it means! :-)


Although I also like descriptive names generally, I prefer for the
standard library short ones.
I am too tired to type all the type
using namespace std::TechnicalLibraryReport1;
or something, in local scopes all the time. Unless you are of the style
to use such using statements only once in the global namespace, but this
is not good style.


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: {AGUT2}=IWIK= | last post by:
Hello all, It's my fisrt post here and I am feeling a little stupid here, so go easy.. :) (Oh, and I've spent _hours_ searching...) I am desperately trying to read in an ASCII...
4
by: csx | last post by:
Hi all, I have the following code, which at the moment is a vector of arrays. But unfortunately, it doesnt do what I want. Basically, here is an example in Java that I want to work in C++. var...
1
by: Matt Garman | last post by:
What is the "best" way to copy a vector of strings to an array of character strings? By "best", I mean most elegantly/tersely written, but without any sacrifice in performance. I'm writing an...
4
by: Oliver Gebele | last post by:
/* OK, after years i'm still more into C; but i already do understand some C++. And there are still many things about the STL which i do not know... I try to put 8-character-arrays in a...
8
by: Hagen | last post by:
Hi, I have a question that you probably shouldn´t worry about since the compiler cares for it, but anyways: When you run your compiler with optimization turned on (eg. g++ with -Ox flag) and...
24
by: simon | last post by:
Hi, First some background. I have a structure, struct sFileData { char*sSomeString1; char*sSomeString2;
17
by: Havatcha | last post by:
Does anyone have a benchmark for the processing overhead of the STL Vector class, vs a C style array? I would dearly love to use Vectors, but am paranoid about slowing my real-time code down. Can...
4
by: Jim Langston | last post by:
I'm using a function like this: char TextBuffer; jGet_DropDown_Selected_Text( cc.ddSex, TextBuffer); Where the function is filling in the text buffer. I don't have access to the actual...
4
by: Bobrick | last post by:
Hi. I'm in the process of making a GUI for a function someone else wrote, and i've come across a type i'm unfamiliar with, namely "std::vector<unsigned char>". I need to get the contents of this...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.