how to fill vector with char* strings

This is a discussion on how to fill vector with char* strings within the C++ Programming forums, part of the General Programming Boards category; hello all im trying to add char* text into std::vector but im keep getting compilations errors , basally i need ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    58

    how to fill vector with char* strings

    hello all
    im trying to add char* text into std::vector
    but im keep getting compilations errors , basally i need to keep dynamically container that holds char* strings

    Code:
    static std::vector<char*> v;
    ....
    ....
    char *name =  getName();
    setVector(name);
    //then i have the inline function that handle the vector operation
    
     
    inline void setVector(const char* c){
        myClass::v.push_back(c);
    }
    the error im keep getting is :
    error C2664: 'push_back' : cannot convert parameter 1 from 'const char *' to 'const char &'
    Reason: cannot convert from 'const char *' to 'const char'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast

    what im doing wrong here ?
    is it the best way to keep char* text in container ?

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,593
    Nope, the best way is std::string.
    std::vector<std::string>
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    58
    andif i like to have dynamic structure to contain char* what can i do ?

  4. #4
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Code:
    // just make a vector of string
    vector<string> v;
    
    // and when you need char *, just do this
    v[index].c_str();

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by manav View Post
    Code:
    // and when you need const char *, just do this
    v[index].c_str();
    Slight correction.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> andif i like to have dynamic structure to contain char* what can i do

    I absolutely agree that it makes no sense to use a vector<char *> when a vector<string> is an option. However, if you did need a container of char*, you should manage the memory of the char* strings, using new to allocate each string in the vector, using strcpy to copy the strings into the vector, and using delete[] to free the memory when strings will be removed from the vector.

    If you wanted only to hold string literals that won't change and are known at compile time, you can have a vector<const char*> and not worry about memory management.

    But really, why would you want vector<char *>?

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Or if you need to pass the strings to a C API which will modify them, you could use:
    Code:
    std::vector< std::vector<char> >

  8. #8
    Registered User
    Join Date
    Mar 2008
    Posts
    58
    well all my application is using char* strings ...
    what is the difference between using char* and std::string ?
    only backward culpability history reasons ?

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,593
    They are different things.
    Char* is a pointer to char.
    std::string is a class that manages a string.

    Note that string literals such as "hi" is const char* and not char*.
    If you use char*, then you must allocate memory using new/malloc and free later. String literals aren't modifiable.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> only backward culpability history reasons ?

    Sort of. Null terminated character arrays are how strings are implemented in C. C++ used them, too, originally, but when the language was standardized ten years ago they included a standard string class.

    For the C++ programmer, the string class means you don't have to manage memory (and handle other string-related tasks). This makes it safer, easier to use, and harder to break.

  11. #11
    Registered User
    Join Date
    Mar 2008
    Posts
    58
    what about size's ?
    application that using only char* string will be smaller then one that using string class?

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    It depends. On most applications the size difference will not be noticeable. If you're already using vectors (which is generally a good idea), then I doubt you'll have a problem.

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Chances are, your app might even be faster if it uses std::string, due to some implementation tricks.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #14
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Quote Originally Posted by umen242 View Post
    what about size's ?
    application that using only char* string will be smaller then one that using string class?
    It depends on the implementation and the compiler, and there aren't any cases I'm aware of where the size difference between using a char array (C-style string) and a String (C++) mattered. A good compiler will optimize away so many things that the differences are minimal.

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Lithorien View Post
    It depends on the implementation and the compiler, and there aren't any cases I'm aware of where the size difference between using a char array (C-style string) and a String (C++) mattered. A good compiler will optimize away so many things that the differences are minimal.
    Obviously, if you have MANY, MANY short strings [and I'm not talking about hundreds or thousands, but rather 100000 or more, and strings that are sort of 5-10 characters], then you may find that there's a benefit in storing a char * rather than a string object. As soon as the string starts being longer, then it's meaningless to care about the size of the object holding the string, since the overhead is now quite small compared to the string itself. And naturally, if you only have a few hundred or thousands of strings, then storage space of the object itself will probably be so small that it doesn't make any difference.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-27-2008, 03:39 AM
  2. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 02:24 PM
  3. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07:07 PM
  4. Fill an array with strings from file
    By niponki in forum C Programming
    Replies: 12
    Last Post: 08-01-2005, 06:50 PM
  5. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21