byte array (vector)

This is a discussion on byte array (vector) within the C++ Programming forums, part of the General Programming Boards category; I have a byte array (vector <char> and I want to copy each byte to string): The code: Code: using ...

  1. #1
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630

    byte array (vector)

    I have a byte array (vector <char> and I want to copy each byte to string):

    The code:

    Code:
    using namespaces std;
    vector <char> v;
    v.resize(20);
    //..fill the vector..
    
    // now copy
    string str;
    copy(v.begin(), v.end(), str.begin());
    The program crashes at the copy line.. What am I doing wrong?

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,436
    something's probably wrong in the "fill the vector" section of your code. It should work

    Regardless:

    Code:
    using namespaces std;
    Remove that 's'
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Code:
    int main( void )
    {
    	std::vector <char> v;
    
    	for ( int i='a'; i<'n'; i++ )
    		v.push_back( i );
    
    	std::string str;
    	str.insert( str.begin(), v.begin(), v.end() );
    
    	std::cout<< '\n' << str;
    
    	return 0;
    
    }
    Last edited by twomers; 09-25-2006 at 12:04 PM.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,436
    Or... much easier, you can build the string from the vector using one of the constructor overloads.

    Code:
    string str(v.begin(), v.end());
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Quote Originally Posted by Mario F.
    Or... much easier, you can build the string from the vector using one of the constructor overloads.

    Code:
    string str(v.begin(), v.end());
    Nice!! I didn't know that!! Shame it's not (yet) possible to do it the other way around :/

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,436
    Code:
    string str = "aaaaaaaaaa";
    vector<char> v(str.begin(), str.end());
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    FWIW, with a slight modification the original copy function could have been used as well.
    Code:
    // now copy
    string str;
    copy(v.begin(), v.end(), inserter(str,str.end()) );
    "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

  8. #8
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,436
    Yes. I was actually trying to understand why it wasn't working and experienced a situation where the copy does apparently nothing.

    I'm not familiar with that function but I guess it returns an insert iterator, correct?
    Cppreference makes no mention to the fact the last argument needs to be one, it seems.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Resource Management question..
    By Raigne in forum C++ Programming
    Replies: 37
    Last Post: 03-08-2008, 08:36 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM
  4. error: identifier "byte" is undefined.
    By Hulag in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2003, 04:46 PM
  5. Operators for 3D Vector Mathematics
    By Anarchist in forum C++ Programming
    Replies: 10
    Last Post: 01-31-2003, 06:33 PM

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