inserting data in a std::queue

This is a discussion on inserting data in a std::queue within the C++ Programming forums, part of the General Programming Boards category; Hello I want to insert elements from some vector to queue.. What I tried is: Code: std::copy(vector.begin(), vector.end(), std::back_inserter(queue)); But ...

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

    inserting data in a std::queue

    Hello

    I want to insert elements from some vector to queue..

    What I tried is:

    Code:
    std::copy(vector.begin(), vector.end(), std::back_inserter(queue));
    But this will give me an error: error C2039: 'push_back' : is not a member of 'std::queue<_Ty>'
    I guess because queue uses push() instead of push_back();

    Which method is best alternative in this case?

    Thanks a lot for help!

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    The best alternative is not to use std::queue. std::queue is an adapter container which sits on top of an underlying container, and presents a different interface -- as you have seen. If you want to use all the standard container interfaces, just use a basic container which is capable of queue-like behavior, i.e., std::list or std::deque.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    Maybe you meant to use deque instead of queue? I believe queue is implemented with deque by default anyway.

    And I'm still not convinced copying data from one container to another is right for you.

  4. #4
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Isnt std::deque something similar to std::queue?

  5. #5
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by Daved View Post
    And I'm still not convinced copying data from one container to another is right for you.
    I send serialized data (std::vector) over the TCP connection, then I unserialize the message class and I want to copy data out of it (so from vector to another container) so that message object can be destroyed.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Well think about how a queue works.
    Like a checkout line at a supermarket. People aren't supposed to sneak into the middle of the line.
    Last edited by cpjust; 04-02-2008 at 10:38 AM. Reason: Oops, I gave a stack analogy at first.

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by l2u View Post
    Isnt std::deque something similar to std::queue?
    I wouldn't call it similar to queue, I'd call it more generic than queue. A std::queue could, in theory, be built on top of either a std::list or a std::deque. The point is, std::queue is not a real container, it just wraps the real container and presents a simplified interface.

    If you don't want the simplified interface and want to deal directly with standard containers, then don't use std::queue, because it's not a standard container -- it's a standard container ADAPTOR.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    You are sending a vector over a TCP connection? Why not just send the raw data? It is easy to access from the vector and you wouldn't have to worry that the other end has a different implementation of vector.

    When you receive the raw data, store it directly in the container that you want to use it with. The vector will often be your best choice for that. If you have a reason to use another container besides vector, then be aware that the time it takes to copy the data might offset the advantage of switching to a different container.

  9. #9
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by Daved View Post
    You are sending a vector over a TCP connection? Why not just send the raw data? It is easy to access from the vector and you wouldn't have to worry that the other end has a different implementation of vector.

    When you receive the raw data, store it directly in the container that you want to use it with. The vector will often be your best choice for that. If you have a reason to use another container besides vector, then be aware that the time it takes to copy the data might offset the advantage of switching to a different container.
    Isnt serialized data (boost::serialization) something like raw data? I think thats the most sensible way to send container data over the connection..

    The reason why I decided to go for deque/queue is because it has pop_front()/back methods which I need to use.

  10. #10
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by l2u View Post
    Isnt serialized data (boost::serialization) something like raw data? I think thats the most sensible way to send container data over the connection..
    NO! The whole point of serialization is to not send "raw" data, because the meaning of raw data changes from platform to platform -- endianness, alignment, character sets, etc. Serialized data is definitely not "raw."

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    >> I think thats the most sensible way to send container data over the connection.
    I didn't realize you were using boost::serialization, I thought you were sending the vector directly.

    I'm not familiar enough with that library to know whether it makes sense, but it still seems fishy. Also, if you are destroying the message object, then swapping it's vector with a local vector might be much more efficient than copying (but again it depends on the details of how your message object is implemented).

  12. #12
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by brewbuck View Post
    NO! The whole point of serialization is to not send "raw" data, because the meaning of raw data changes from platform to platform -- endianness, alignment, character sets, etc. Serialized data is definitely not "raw."
    I dont do binary serialization - its text instead and boost says its portable. So I dont think theres an issue. Beside that, all data I send over is serialized, so it would be quite tricky to implement something over my current code so that I could send 'raw data' for containers.

  13. #13
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by l2u View Post
    I dont do binary serialization - its text instead and boost says its portable. So I dont think theres an issue.
    There is an issue if the destination host has a different character set than the source. Not everything in the world is ASCII -- there's quite a few systems out there still using EBCDIC. So even making everything text doesn't guarantee complete portability.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting hex byte into a data
    By jaqquery in forum C Programming
    Replies: 4
    Last Post: 02-25-2009, 01:18 AM
  2. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 02:27 AM
  3. simultaneously waiting for data on FIFO and UDP using select call
    By yogesh3073 in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-05-2007, 08:53 AM
  4. Help with Algorithm for inserting data into Binary Tree
    By bcianfrocca in forum C++ Programming
    Replies: 2
    Last Post: 05-09-2005, 04:36 AM
  5. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM

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