copying strings efficiently?

Discussion in 'C++' started by Mark van Heeswijk, Aug 29, 2003.

  1. 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
     
    Mark van Heeswijk, Aug 29, 2003
    #1
    1. Advertising

  2. "Mark van Heeswijk" <> 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
     
    John Harrison, Aug 29, 2003
    #2
    1. Advertising

  3. > > 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
     
    Newsnet Customer, Aug 30, 2003
    #3
  4. "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
     
    John Harrison, Aug 31, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lafer

    problems with copying/editing strings

    Lafer, Feb 18, 2004, in forum: C Programming
    Replies:
    4
    Views:
    563
    Joona I Palaste
    Feb 19, 2004
  2. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    768
    Malcolm
    Jun 24, 2006
  3. per
    Replies:
    7
    Views:
    319
    7stud
    Sep 6, 2009
  4. Brad
    Replies:
    7
    Views:
    1,301
    Jorgen Grahn
    Jun 25, 2010
  5. Travis Parks

    Efficiently Building Strings

    Travis Parks, May 22, 2012, in forum: C++
    Replies:
    29
    Views:
    682
    Pavel
    May 31, 2012
Loading...

Share This Page