VoolDD5's blog

By VoolDD5, 10 years ago, In English

Hi!
can you please tell me why people use vector while deque do the same with much more features :(

  • Vote: I like it
  • +12
  • Vote: I do not like it

| Write comment?
»
10 years ago, # |
  Vote: I like it -19 Vote: I do not like it

Really? I didn't know you can find/change the value of an arbitrary element of a deque in O(1)...

  • »
    »
    10 years ago, # ^ |
    Rev. 3   Vote: I like it +16 Vote: I do not like it

    take a look at here:http://www.cplusplus.com/reference/deque/deque/operator[]/
    Says Complexity is constant.

    • »
      »
      »
      10 years ago, # ^ |
        Vote: I like it -29 Vote: I do not like it

      Lolwut. Its' not really a queue anymore...

      • »
        »
        »
        »
        10 years ago, # ^ |
          Vote: I like it +9 Vote: I do not like it

        Its not a queue.
        It's deque. :D

        • »
          »
          »
          »
          »
          10 years ago, # ^ |
            Vote: I like it -35 Vote: I do not like it

          Which is a portmanteau of "double ended queue". See the word "queue" in there?

          • »
            »
            »
            »
            »
            »
            10 years ago, # ^ |
              Vote: I like it +25 Vote: I do not like it

            That was just kidding.
            In fact in c++ standard the name "deque" is used just bcz of efficient push and pop from both ends.

            • »
              »
              »
              »
              »
              »
              »
              10 years ago, # ^ |
                Vote: I like it -19 Vote: I do not like it

              Dunno, I don't find nitpicking over words very funny...

              Good question, then: why is vector still in use at all? It might have other benefits like being faster or more memory saving, but not on a scale that would matter for most problems...

              • »
                »
                »
                »
                »
                »
                »
                »
                10 years ago, # ^ |
                  Vote: I like it +17 Vote: I do not like it

                bcz data is not stored in a continuous structure and it makes deque impossible to use for C libraries.

      • »
        »
        »
        »
        10 years ago, # ^ |
          Vote: I like it +3 Vote: I do not like it

        I think it's implemented using vector, isn't it?

        • »
          »
          »
          »
          »
          10 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          Yeah, it is. But since the element indexing changes, you can't just take the [] operator from vector directly... and since it's just supposed to have the deque functionality, I didn't think element access would be implemented.

          It's not like it's hard to write my own deque over a vector.

          • »
            »
            »
            »
            »
            »
            10 years ago, # ^ |
              Vote: I like it +5 Vote: I do not like it

            No.
            deque uses paging technic, which dose not keeps data continuously. but vector uses array. and in array data is stored continuously.

            • »
              »
              »
              »
              »
              »
              »
              10 years ago, # ^ |
                Vote: I like it 0 Vote: I do not like it

              The STL deque does. I could just take a 2 vectors and connect them by their starting ends, plus take 1 variable that indicates the true positions of front and back of the resulting deque.

              • »
                »
                »
                »
                »
                »
                »
                »
                10 years ago, # ^ |
                  Vote: I like it 0 Vote: I do not like it

                deque is implemented as a vector of vectors.

                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  10 years ago, # ^ |
                    Vote: I like it 0 Vote: I do not like it

                  For example. Also possible.

                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  10 years ago, # ^ |
                    Vote: I like it +3 Vote: I do not like it

                  then we don't use vector anymore...?

                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  10 years ago, # ^ |
                    Vote: I like it 0 Vote: I do not like it

                  deque is faster for growing size.
                  but vector is faster in other features.

                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  10 years ago, # ^ |
                    Vote: I like it 0 Vote: I do not like it

                  sry, I don't understand what features do you mean.

                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  10 years ago, # ^ |
                    Vote: I like it 0 Vote: I do not like it

                  for example accessing an element of vector is faster than accessing an element of deque.

                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  10 years ago, # ^ |
                    Vote: I like it 0 Vote: I do not like it

                  I thought they are both O(1), isn't it?

                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  10 years ago, # ^ |
                    Vote: I like it 0 Vote: I do not like it

                  but they have a constant difference.

                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  10 years ago, # ^ |
                    Vote: I like it 0 Vote: I do not like it

                  Do you know exactly how much vector is faster? for example ~4 times...

                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  10 years ago, # ^ |
                    Vote: I like it 0 Vote: I do not like it

                  according to this link:http://www.gotw.ca/gotw/054.htm
                  in builtin types around 2 or 3 times faster. and in complex types around 1.5 faster.

                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  10 years ago, # ^ |
                    Vote: I like it -16 Vote: I do not like it

                  most recursive comments ever

  • »
    »
    10 years ago, # ^ |
    Rev. 3   Vote: I like it +3 Vote: I do not like it

    Yes! really :D, I'm wondering too

»
10 years ago, # |
  Vote: I like it +30 Vote: I do not like it

Both vectors and deques provide a very similar interface and can be used for similar purposes, but internally both work in quite different ways: While vectors use a single array that needs to be occasionally reallocated for growth, the elements of a deque can be scattered in different chunks of storage

from cplusplus.com

Therefore the indexing operator[]() could take more time because you need to determine the certain chunk of memory at first and then the position of an element in this chunk.

And second point — it might be very confusing for the reader of the code to see a deque where he expected one of more standard array types.

  • »
    »
    10 years ago, # ^ |
      Vote: I like it +8 Vote: I do not like it

    Finally someone on codeforces who pays attention for readability :D