473,811 Members | 3,152 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<cha r[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 2422
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<cha r[8]> m_titles;

The declaration compiles but then calling reserve or resize leads to
obscure compiler errors.
Yeah, this fails both copyconstuctibi lity 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<cha r[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******** ************@co mcast.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<cha r[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<cha r[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::arra y' is a class
template for fixed size arrays. The replacement would look like this:

| std::vector<std ::tr1::array<ch ar, 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.co m> <http://www.dietmar-kuehl.de/>
<http://www.contendix.c om> - 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::arra y' is a class
template for fixed size arrays. The replacement would look like this:

| std::vector<std ::tr1::array<ch ar, 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.names paces) 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.co m> <http://www.dietmar-kuehl.de/>
<http://www.contendix.c om> - Software Development & Consulting

Jul 22 '05 #8

"Dietmar Kuehl" <di***********@ yahoo.com> wrote in message
news:11******** **************@ c13g2000cwb.goo glegroups.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.names paces) 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.co m> <http://www.dietmar-kuehl.de/>
<http://www.contendix.c om> - 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.names paces) 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.co m> <http://www.dietmar-kuehl.de/>
<http://www.contendix.c om> - Software Development & Consulting

Jul 22 '05 #10

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

Similar topics

9
3212
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 "stereolithography" file (*.STL) into my program. This has the following syntax... Begin STL Snippet **********
4
2490
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 Levels = new Array() Levels = new makearray(1,1,'b',1); Levels = new makearray(2,2,'g',1); Levels = new makearray(3,5,'h',1);
1
18023
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 application using C++ and the STL for handling my data. Unfortunately, I must interact with a (vanilla) C API. I use vectors of strings (for simplicity and less memory hassle), but the function calls for this API require arrays of character...
4
3169
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 vector and when compiling on my Linux-box i get too many errors to include here.
8
1953
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 your program gets significantly faster than without, did you write bad code/ have a bad design? Cause what happens in those optimization steps is, I think, mostly
24
2940
by: simon | last post by:
Hi, First some background. I have a structure, struct sFileData { char*sSomeString1; char*sSomeString2;
17
25169
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 anyone reassure?
4
2392
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 function to change it to a std::string (it's a library function) so I was thinking, well, I could use a std::vector<charinstead of a char array, but would that actually gain me anything at all? I mean, what's the difference between calling it like the...
4
20140
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 variable into a form I can display in a text box, but i'm not sure what to expect inside of the variable, whether I can just treat it like an array e.t.c. Any help would be appreciated. Thanks,
0
9730
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10651
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10392
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10403
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10136
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7671
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6893
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5693
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4341
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.