Thread: Vector<char> push_back

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

    Vector<char> push_back

    1. How to append at the end of one vector<char> another one with push_back?
    I know that v.insert() works too but I wonder if it can be done with push_back.
    2. Does it matter if I declare vector<unsigned char> or just vector<char> if I want to store binary string in it?
    Code:
    int main()
    {
         vector<char> v(5, '0');
         vector<char> v2;
    
         v2.push_back(&v.at(0));
         v2.push_back(v);
    }
    Last edited by Ducky; 06-15-2013 at 04:05 AM.
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Ducky View Post
    1. How to append at the end of one vector<char> another one with push_back?
    I know that v.insert() works too but I wonder if it can be done with push_back.
    push_back() appends only a single element so use a loop calling push_back() for each element of the other vector you intend to append. Probably easier to use insert() though.

    Quote Originally Posted by Ducky View Post
    2. Does it matter if I declare vector<unsigned char> or just vector<char> if I want to store binary string in it?
    It depends on how you have obtained that "binary string", and what you intend to do with it after storing it. If you read it from a standard ifstream (assuming the stream has been opened in binary mode), data is typically read as char.

    It is implementation defined whether a (unadorned) char is signed or unsigned. If it is signed, the behaviour is undefined on overflow and underflow (e.g. what happens if a char holds its maximum possible value and it is then incremented). If char is unsigned, the behaviour is well defined on overflow (i.e. modulo arithmetic). If you will do operations on the data that potentially overflow the individual characters, it is often considered better to work with unsigned char.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Thanks Grumpy.
    1. I want to append only one part of a vector<char> at the end of the other so I better use a loop to get the part I want to copy I guess cause I dont think that insert() can do this.

    2. Since Vector is not a pointer I guess there is no risk of overflow.
    Using Windows 10 with Code Blocks and MingW.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I think you can use resize() and std::copy
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Ducky View Post
    1. I want to append only one part of a vector<char> at the end of the other so I better use a loop to get the part I want to copy I guess cause I dont think that insert() can do this.
    Yes it can. One of the forms of insert() accepts two input iterators that specify the beginning and end of an input range.

    Quote Originally Posted by Ducky View Post
    2. Since Vector is not a pointer I guess there is no risk of overflow.
    You clearly misunderstood the point made. I did not refer in any way to a vector or pointer overflowing. I referred to the possibility of the value of characters overflowing.

    Code:
    #include <limits>
    
    int main()
    {
         char c = std::numeric_limits<char>::max();
         ++c;    //  This overflows c.  Results are undefined if char is signed
    
         unsigned char u = std::numeric_limits<unsigned char>::max();
         ++u;    //  this overflows u.   The result is zero since u is of type unsigned char
    }
    As I said, what works depends on what you want to do with the characters in the vector.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cannot push_back string at the end of vector
    By fnoyan in forum C++ Programming
    Replies: 1
    Last Post: 05-23-2013, 10:36 PM
  2. vector push_back array
    By Tropod in forum C++ Programming
    Replies: 3
    Last Post: 10-15-2010, 04:46 PM
  3. Error from vector push_back()
    By The Wazaa in forum C++ Programming
    Replies: 7
    Last Post: 03-11-2006, 01:15 PM
  4. vector.push_back(anotherVector)
    By boojus in forum C++ Programming
    Replies: 2
    Last Post: 11-22-2003, 05:07 PM
  5. vector<bool> push_back
    By ygfperson in forum C++ Programming
    Replies: 4
    Last Post: 03-05-2003, 08:48 PM