Thread: Copying string vector to char vector

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

    Copying string vector to char vector

    What is wrong with that code?
    It opens up xmemory file as an error when I compile it.
    The problem is on the <char>vector declaration.

    Code:
    #include <iterator>
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <iostream>
    using namespace std;
    
    int main()
    {
        vector<string> v;
    
        v.push_back("aaa");
        v.push_back("bbb");
    
        vector<char> cv(v.begin(), v.end());
        cout << "Size " << cv.size() << endl;
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Well it doesn't make sense. You cant to copy the string "aaa" into the first char and "bbb" into the second char.
    But those are two strings, not two chars!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Well then how to copy a string vector to a char vector.
    I looked it up and apparently it's working that way for others.
    Using Windows 10 with Code Blocks and MingW.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Ducky View Post
    Well then how to copy a string vector to a char vector.
    I looked it up and apparently it's working that way for others.
    Depends on what you expect the result to look like. There's no automatic way to do it, because there's no reasonable default way to convert a string to a single char.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Thanks.
    Then I'll use a string instead of vector string.
    Last edited by Ducky; 07-07-2013 at 03:50 PM.
    Using Windows 10 with Code Blocks and MingW.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Are you sure the "others" weren't talking about a vector of vector of chars instead of just a simple vector of chars?

    Code:
    vector<vector<char> > cv;
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What are you trying to do?
    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.

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Yes 'others' were transforming string into vector<char>. I missed that.

    So I just replaced the vector<string> with a std::string and that can be transformed into vector<char>. Solved.
    Using Windows 10 with Code Blocks and MingW.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Again, why?
    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.

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Because I have to append a bunch of char arrays and string literals and then send it with a function that takes a vector<char>.

    And it seems to be much easier to put them in a string than to use v.insert().
    Last edited by Ducky; 07-09-2013 at 07:07 AM.
    Using Windows 10 with Code Blocks and MingW.

  11. #11
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Why not to use the std::string class?
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  12. #12
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    You missed one of my posts.
    That's what I use now.
    Last edited by Ducky; 07-09-2013 at 07:18 AM.
    Using Windows 10 with Code Blocks and MingW.

  13. #13
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    oh I see. Well if I were you, I would modify the function too, so that it would use string instead of vector<char>
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Ducky View Post
    Because I have to append a bunch of char arrays and string literals and then send it with a function that takes a vector<char>.

    And it seems to be much easier to put them in a string than to use v.insert().
    Why does the function take a vector of chars?
    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.

  15. #15
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    It's the Win32 API send() function.
    Don't know if it would work as well with std::string?
    Using Windows 10 with Code Blocks and MingW.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting string vector to integer vector
    By CPlus in forum C++ Programming
    Replies: 4
    Last Post: 05-08-2010, 05:43 AM
  2. Replies: 9
    Last Post: 04-04-2008, 12:41 PM
  3. copying vector to list
    By l2u in forum C++ Programming
    Replies: 13
    Last Post: 04-01-2008, 02:28 PM
  4. std::vector<char> vs. std::string
    By drrngrvy in forum C++ Programming
    Replies: 11
    Last Post: 12-15-2006, 05:06 PM
  5. connect vector<int> and vector<char>
    By hdragon in forum C++ Programming
    Replies: 1
    Last Post: 04-27-2006, 01:16 PM