Thread: Initializing string from a vector

  1. #1
    Registered User
    Join Date
    Nov 2013
    Location
    Norway
    Posts
    40

    Initializing string from a vector

    Hello, I am learning about ways of initializing strings from other containers. One of the tasks is to initialize a string from a char vector, but something goes wrong, and the book doesnt show an example on how to the initialization with another container besides an array.

    Code:
    vector<char> cvec = { 'H', 'i' };
        string s1(cvec, 2);
    I think it should look something like this.
    I have tried to write it 3-4 different ways, but nothing has worked.

    If anyone could show me the correct syntax, I would be very grateful!
    "Derp, derp, derp" - Me

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    std::string s1(cvec.begin(), cvec.end()); // Iterator version
    // Or
    std::string s1(&cvec[0], cvec.size()); // C-style string + size version
    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
    Nov 2013
    Location
    Norway
    Posts
    40
    Thank you! I think it was the second one the book was asking for
    "Derp, derp, derp" - Me

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vector initializing first element
    By Ducky in forum C++ Programming
    Replies: 15
    Last Post: 06-19-2012, 01:20 PM
  2. initializing a vector of structs in class constructor
    By edishuman in forum C++ Programming
    Replies: 2
    Last Post: 03-14-2012, 10:54 PM
  3. Initializing 2D Vector?
    By nacho4d in forum C++ Programming
    Replies: 2
    Last Post: 02-09-2010, 06:11 AM
  4. Initializing a Bit string
    By sarathius in forum C Programming
    Replies: 2
    Last Post: 02-11-2008, 07:09 AM
  5. Initializing a C String
    By cpluspluser in forum C++ Programming
    Replies: 3
    Last Post: 04-09-2003, 11:34 PM