Like Tree2Likes
  • 1 Post By laserlight
  • 1 Post By Elysia

Vector element to char*

This is a discussion on Vector element to char* within the C++ Programming forums, part of the General Programming Boards category; Is there a simpler method? Code: vector<string> Size; char *s = strdup(Size[i].c_str()); long long num = _strtoi64(s, NULL, 10);...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    796

    Vector element to char*

    Is there a simpler method?

    Code:
            vector<string> Size;
            char *s = strdup(Size[i].c_str());
            long long num = _strtoi64(s, NULL, 10);
    Compiler MSVC++ 2013 with Code::Blocks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,747
    If your standard library implementation conforms sufficiently to C++11, you could write:
    Code:
    vector<string> Size;
    // ...
    long long num = stoll(Size[i]);
    Ducky likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,621
    You don't need char* in the first place. _strtoi64 (which is bad function to use, btw) takes a const char*, because it doesn't change its parameters. Therefore, simply calling c_str() should suffice.
    Also, to duplicate a string, all you need is to simply declare another string and assign the old one to it.
    Ducky likes this.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. vector<vector<int> > access element.
    By nimitzhunter in forum C++ Programming
    Replies: 0
    Last Post: 01-23-2011, 04:14 AM
  2. vector remove element of element
    By Ducky in forum C++ Programming
    Replies: 6
    Last Post: 09-12-2010, 03:24 PM
  3. Replies: 9
    Last Post: 04-04-2008, 12:41 PM
  4. max element in vector
    By acosgaya in forum C++ Programming
    Replies: 3
    Last Post: 10-23-2006, 04:07 PM
  5. Deleting an element from a vector
    By earth_angel in forum C++ Programming
    Replies: 3
    Last Post: 07-28-2005, 11:45 AM

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