Thread: assign <char>vector to global std::string

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

    assign <char>vector to global std::string

    This is how you can assign <char>vector to std::string when you create the string.

    Code:
    std::vector<char> cVec(length);
    . . .
    string str(cVec.begin(),cVec.end());
    But how do you convert it to a global string? Is it possible or we have to create a second string first?
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What is a "global" string?
    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
    Dec 2007
    Posts
    932
    Quote Originally Posted by Elysia View Post
    What is a "global" string?
    Code:
    string str; // global variable std::string
    
    int main()
    {
    
    ...
    
    str <- assign <char> vector;
    
    ...
    
    
    }
    Using Windows 10 with Code Blocks and MingW.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Why the global variable?

    Do you mean something like:

    Code:
    #include <string>
    #include <vector>
    #include <iostream>
    
    using namespace std;
    
    
    int main()
    {
       // Why a global?
       string str;
    
       char c = 32;
       std::vector<char> cVect;
       for( int i; i < 5;  ++i)
          cVect.push_back(++c);
    
       // Print the vector elements.
       for( auto itr : cVect)
          cout << itr << " ";
       std::cout << "\n\n";
    
       // Copy the vector to the string.
       for(auto itr : cVect)
          str += itr;
    
       // Print the string.
       cout << str << std::endl;
    
       return(0);
    }
    Or you could also use std::copy() if you desired.

    Jim

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Yes, or a string declared in main(), doesn't matter.

    I tried copy() but it wont copy anything.

    Code:
    str.copy(&cVec[0], cVec.size(), 0);
    Should I copy it char by char? Could be time expensive for a large file.
    Last edited by Ducky; 03-17-2015 at 11:02 AM.
    Using Windows 10 with Code Blocks and MingW.

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> assign <char>vector to global std::string
    Your verb is right on the money: basic_string::assign - C++ Reference

    gg

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Quote Originally Posted by Codeplug View Post
    >> assign <char>vector to global std::string
    Your verb is right on the money: basic_string::assign - C++ Reference

    gg
    Hahaha, yeah, assign() is working! Thanks Codeplug!

    And to you to Jim!
    Using Windows 10 with Code Blocks and MingW.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Ducky View Post
    I tried copy() but it wont copy anything.

    Code:
    str.copy(&cVec[0], cVec.size(), 0);
    Do you even know how to use std::string::copy? This copies characters FROM string, not TO string.
    Regardless, what you can do it either

    a) Construct a string with the correct length and read into it directly:
    std::string s(my_length, '\0');
    // Read into &s[0].

    b) If you must assign it to another string that already exists, you can resize it and read directly into it:
    my_str.resize(my_length);
    // Read into &s[0].

    c) With C++11, you can also use a temporary cheaply by moving the result into dest:
    std::string s(my_length, '\0');
    // Read into &s[0].
    dst = std::move(s);
    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.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I note that the first two of Elysia's suggestions in post #8 pertain to doing away with the std::vector<char> and reading into the std::string directly. The third suggestion would be suitable if you really must have a std::vector<char>, but then using the assign member function would be more straightforward.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying string vector to char vector
    By Ducky in forum C++ Programming
    Replies: 21
    Last Post: 07-10-2013, 11:22 AM
  2. Assign part of char array to vector
    By Ducky in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2013, 12:01 PM
  3. Replies: 3
    Last Post: 10-03-2012, 01:23 AM
  4. assign a string to char pointer
    By seaking1 in forum C Programming
    Replies: 8
    Last Post: 12-08-2009, 02:54 PM
  5. std::vector<char> vs. std::string
    By drrngrvy in forum C++ Programming
    Replies: 11
    Last Post: 12-15-2006, 05:06 PM