Like Tree13Likes

Copying string vector to char vector

This is a discussion on Copying string vector to char vector within the C++ Programming forums, part of the General Programming Boards category; What is wrong with that code? It opens up xmemory file as an error when I compile it. The problem ...

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

    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;
    Compiler MSVC++ 2013 with Code::Blocks.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    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!
    Ducky likes this.
    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
    793
    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.
    Compiler MSVC++ 2013 with Code::Blocks.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,032
    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.
    Ducky likes this.
    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
    793
    Thanks.
    Then I'll use a string instead of vector string.
    Last edited by Ducky; 07-07-2013 at 03:50 PM.
    Compiler MSVC++ 2013 with Code::Blocks.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    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;
    Ducky and manasij7479 like this.
    "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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    What are you trying to do?
    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.

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    793
    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.
    Compiler MSVC++ 2013 with Code::Blocks.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Again, why?
    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.

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    793
    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.
    Compiler MSVC++ 2013 with Code::Blocks.

  11. #11
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    Why not to use the std::string class?
    Ducky likes this.
    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
    793
    You missed one of my posts.
    That's what I use now.
    Last edited by Ducky; 07-09-2013 at 07:18 AM.
    Compiler MSVC++ 2013 with Code::Blocks.

  13. #13
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    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
    793
    It's the Win32 API send() function.
    Don't know if it would work as well with std::string?
    Compiler MSVC++ 2013 with Code::Blocks.

Page 1 of 2 12 LastLast
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, 04: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

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