Thread: byte array (vector)

  1. #1
    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
    Ireland
    Posts
    8,446
    something's probably wrong in the "fill the vector" section of your code. It should work

    Regardless:

    Code:
    using namespaces std;
    Remove that 's'
    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,273
    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
    Ireland
    Posts
    8,446
    Or... much easier, you can build the string from the vector using one of the constructor overloads.

    Code:
    string str(v.begin(), v.end());
    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,273
    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
    Ireland
    Posts
    8,446
    Code:
    string str = "aaaaaaaaaa";
    vector<char> v(str.begin(), str.end());
    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,817
    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
    Ireland
    Posts
    8,446
    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.
    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, 09: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, 01:26 AM
  4. error: identifier "byte" is undefined.
    By Hulag in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2003, 05:46 PM
  5. Operators for 3D Vector Mathematics
    By Anarchist in forum C++ Programming
    Replies: 10
    Last Post: 01-31-2003, 07:33 PM