Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to add data to sorted vector?

How to add data to sorted vector?

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestiongraphics
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    alikalik
    wrote on last edited by
    #1

    Hello, I have to add data to a vector in sorted order. For example: 3,7,3,4 must be: 1)3 2)3,7 3)3,3,7 4)3,3,4,7 What is the most quick way to do this? Or maybe other data stuctures like Map would be better for this task?

    CPalliniC R S 3 Replies Last reply
    0
    • A alikalik

      Hello, I have to add data to a vector in sorted order. For example: 3,7,3,4 must be: 1)3 2)3,7 3)3,3,7 4)3,3,4,7 What is the most quick way to do this? Or maybe other data stuctures like Map would be better for this task?

      R Offline
      R Offline
      Rajesh R Subramanian
      wrote on last edited by
      #2

      alikalik wrote:

      Or maybe other data stuctures like Map would be better for this task?

      std::set[^] is a sorted associative container.

      It is a crappy thing, but it's life -^ Carlo Pallini

      A 1 Reply Last reply
      0
      • A alikalik

        Hello, I have to add data to a vector in sorted order. For example: 3,7,3,4 must be: 1)3 2)3,7 3)3,3,7 4)3,3,4,7 What is the most quick way to do this? Or maybe other data stuctures like Map would be better for this task?

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        You may use a multiset [^]. :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        In testa che avete, signor di Ceprano?

        1 Reply Last reply
        0
        • R Rajesh R Subramanian

          alikalik wrote:

          Or maybe other data stuctures like Map would be better for this task?

          std::set[^] is a sorted associative container.

          It is a crappy thing, but it's life -^ Carlo Pallini

          A Offline
          A Offline
          alikalik
          wrote on last edited by
          #4

          Hm... But is it possible to access the element at the specified index in multiset?

          1 Reply Last reply
          0
          • A alikalik

            Hello, I have to add data to a vector in sorted order. For example: 3,7,3,4 must be: 1)3 2)3,7 3)3,3,7 4)3,3,4,7 What is the most quick way to do this? Or maybe other data stuctures like Map would be better for this task?

            S Offline
            S Offline
            Stuart Dootson
            wrote on last edited by
            #5

            Depends on the usage patterns. If the number of lookups is significantly higher than the number of inserts, then I'd just use a sorted vector (as my experiences indicate that a binary search on a sorted vector is significantly faster than use of STL's associative containers in C++). If inserts are more significant than that, use a std::multiset as suggested elsewhere. If using a sorted vector, it helps to delay the sort operation until required, using something like below, rather than blindly sorting on every insert.

            template<class Type>
            class SortedVector
            {
            public:
            SortedVector() sorted_(true) {}
            void insert(Type const& value)
            {
            sorted_ = false;
            data_.push_back(value);
            }
            typename std::vector<Type>::const_iterator find(Type const& value) const
            {
            if (!sorted_)
            {
            std::sort(data_.begin(), data_.end());
            sorted_ = true;
            }
            std::vector<Type>::const_iterator found =
            std::lower_bound(data_.begin(), data_.end(), value);
            return (*found == value)?found:data_.end();
            }
            private:
            mutable bool sorted_;
            mutable std::vector<Type> data_;
            };

            Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups