Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > copying strings efficiently?

Reply
Thread Tools

copying strings efficiently?

 
 
Mark van Heeswijk
Guest
Posts: n/a
 
      08-29-2003
Probably a very basic question but i'm new to c++

What's the most efficient (mem) way to copy string 'a' to string 'b'? Now i
do:

char a[1000];
char* b; // length not known...could be 1000
strcpy(a, b);

Is it really necessary that 'a' always gets allocated the max size of 'b',
or can this be done more efficiently?

Thanks in advance,
Mark


 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      08-29-2003

"Mark van Heeswijk" <blackhawk+> wrote in message
news:diM3b.22215$.. .
> Probably a very basic question but i'm new to c++
>
> What's the most efficient (mem) way to copy string 'a' to string 'b'? Now

i
> do:
>
> char a[1000];
> char* b; // length not known...could be 1000
> strcpy(a, b);
>
> Is it really necessary that 'a' always gets allocated the max size of 'b',
> or can this be done more efficiently?
>
> Thanks in advance,
> Mark
>


The most efficient way to copy a string is to use the std::string class.

#include <string>

std::string a = whatever;
std::string b = a; // copies string a to string b

Don't mess around with char arrays, learn some proper C++ and use the C++
string class.

john


 
Reply With Quote
 
 
 
 
Newsnet Customer
Guest
Posts: n/a
 
      08-30-2003
> > What's the most efficient (mem) way to copy string 'a' to string 'b'?
Now
> i
> > do:
> >
> > char a[1000];
> > char* b; // length not known...could be 1000
> > strcpy(a, b);
> >
> > Is it really necessary that 'a' always gets allocated the max size of

'b',
> > or can this be done more efficiently?
> >
> > Thanks in advance,
> > Mark
> >

>
> The most efficient way to copy a string is to use the std::string class.
>
> #include <string>
>
> std::string a = whatever;
> std::string b = a; // copies string a to string b
>
> Don't mess around with char arrays, learn some proper C++ and use the C++
> string class.



what is the std namespace used for? used for all C++ header files?

asasas


 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      08-31-2003

"Newsnet Customer" <> wrote in message
news:...
> > > What's the most efficient (mem) way to copy string 'a' to string 'b'?

> Now
> > i
> > > do:
> > >
> > > char a[1000];
> > > char* b; // length not known...could be 1000
> > > strcpy(a, b);
> > >
> > > Is it really necessary that 'a' always gets allocated the max size of

> 'b',
> > > or can this be done more efficiently?
> > >
> > > Thanks in advance,
> > > Mark
> > >

> >
> > The most efficient way to copy a string is to use the std::string class.
> >
> > #include <string>
> >
> > std::string a = whatever;
> > std::string b = a; // copies string a to string b
> >
> > Don't mess around with char arrays, learn some proper C++ and use the

C++
> > string class.

>
>
> what is the std namespace used for? used for all C++ header files?
>
> asasas
>


The std namespace is for the C++ standard library, which means things like
std::string, std::cout, std::vector, std::map etc. Any standard header file
without a .h is the C++ standard library.

john


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
confused about copying strings Angus C Programming 4 07-18-2010 09:36 PM
Copying strings Khookie C Programming 6 12-14-2007 10:46 PM
Copying strings WoodHacker Ruby 3 01-19-2007 06:41 PM
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
problems with copying/editing strings Lafer C Programming 4 02-19-2004 10:04 PM



Advertisments