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
    856

    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
    23,796
    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.
    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
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,996
    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