CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2016
    Posts
    2

    Copying Vectors?

    I am trying to copy exactly as it is, a vector<std::string> to a vector<byte> without changing/casting any element. What do i mean? For example this is what it is contained in my vector<std::string>:

    Name:  h5vYVrE.jpg
Views: 114
Size:  23.0 KB

    How could i possibly copy the exact same elements on a vector<byte> ?

    Thanks.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Copying Vectors?

    Your image is way to small to be able to be read.

    A string contains a variable number of characters and a vector contains a variable number of elements. So for each string element of the vector do you want one vector<byte> or do you want one vector<byte> that contains all the chars from the vector<string>? If the latter, how do you want each different element separated?

    What are you trying to accomplish by doing this? For a string, you can iterate over the chars in the string to extract each char in turn.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Jun 2016
    Posts
    2

    Re: Copying Vectors?

    I'm sorry for that img, i post the link to it:
    Here

    What i want to do is copy the exact same elements of the vector<std::string> into the vector<byte>. For example, Element 1 of vector<std::string> is AC so i want on my vector<byte> Element 1: AC.

    I hope i explained myself properly,

    Thanks!

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Copying Vectors?

    Do you want all the string elements from the vector<string> to be copied to the same vector<byte> one after the other?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Copying Vectors?

    Is this something like you are after? I'm still not sure what you actually want - or why?

    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    #include <iterator>
    #include <algorithm>
    using namespace std;
    
    using byte = unsigned char;
    
    int main()
    {
    	const vector<string> vs { "qw", "as", "iu", "oi" };
    
    	for (const auto& s : vs) {
    		vector<byte> vb;
    		for (const auto& b : s)
    			vb.push_back(b);
    
    		copy(vb.begin(), vb.end(), ostream_iterator<byte>(cout, ""));
    		cout << endl;
    	}
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured