Thread: Vector element to char*

  1. #1
    Registered User
    Join Date
    Dec 2007

    Vector element to char*

    Is there a simpler method?

            vector<string> Size;
            char *s = strdup(Size[i].c_str());
            long long num = _strtoi64(s, NULL, 10);
    Windows 10 with Visual Studio 14.0.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    If your standard library implementation conforms sufficiently to C++11, you could write:
    vector<string> Size;
    // ...
    long long num = stoll(Size[i]);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Join Date
    Oct 2007
    Inside my computer
    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.
    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, 05: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