Initializing string from a vector

This is a discussion on Initializing string from a vector within the C++ Programming forums, part of the General Programming Boards category; Hello, I am learning about ways of initializing strings from other containers. One of the tasks is to initialize a ...

  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
    Posts
    22,915
    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, 02:20 PM
  2. initializing a vector of structs in class constructor
    By edishuman in forum C++ Programming
    Replies: 2
    Last Post: 03-14-2012, 11: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-10-2003, 12:34 AM

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