One Container question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pat

    One Container question

    When inserting an number, say X, into a vector, if X already is in the
    vector, then do nothing ( because I do want a repeated X occurs in the
    vector). Otherwise, push X into the container.

    Any efficient way to do this? Because my vector container is very larger.

    Thanks. Pat


  • Gregg

    #2
    Re: One Container question

    "Pat" <Pat@Pat.com> wrote in news:40ad7b43$1 _1@rain.i-cable.com:
    [color=blue]
    > When inserting an number, say X, into a vector, if X already is in the
    > vector, then do nothing ( because I do want a repeated X occurs in the
    > vector). Otherwise, push X into the container.
    >
    > Any efficient way to do this? Because my vector container is very larger.
    >
    > Thanks. Pat[/color]

    If the type of X has (or can have) a relational operator defined on it,
    then you ou can use a std::set<> in parallel with the vector to check for
    existence in the set before adding it to the vector. Otherwise, you will
    have to do a linear search using std::find. Is it possible you don't need
    the vector at all and that what you really want is a set?

    Gregg

    Comment

    • Pat

      #3
      Re: One Container question

      X is double number.
      "Gregg" <gregg@invalid. invalid> ¦b¶l¥ó
      news:Xns94F035F 81D8gregginvali dinvalid@207.69 .154.203 ¤¤¼¶¼g...[color=blue]
      > "Pat" <Pat@Pat.com> wrote in news:40ad7b43$1 _1@rain.i-cable.com:
      >[color=green]
      > > When inserting an number, say X, into a vector, if X already is in the
      > > vector, then do nothing ( because I do want a repeated X occurs in the
      > > vector). Otherwise, push X into the container.
      > >
      > > Any efficient way to do this? Because my vector container is very[/color][/color]
      larger.[color=blue][color=green]
      > >
      > > Thanks. Pat[/color]
      >
      > If the type of X has (or can have) a relational operator defined on it,
      > then you ou can use a std::set<> in parallel with the vector to check for
      > existence in the set before adding it to the vector. Otherwise, you will
      > have to do a linear search using std::find. Is it possible you don't need
      > the vector at all and that what you really want is a set?
      >
      > Gregg[/color]


      Comment

      • Gregg

        #4
        Re: One Container question

        "Pat" <Pat@Pat.com> wrote in news:40ad879b$1 _2@rain.i-cable.com:
        [color=blue]
        > X is double number.[/color]

        Then just use a std::set<double > to keep track of what you've added to the
        vector. You might also consider using std::map<int, double> and not use the
        vector at all. The suitablility of this depends on how often you will be
        looking up entries versus adding them. Perhaps you don't need to look up by
        index at all, in which case just use a set and do away with the vector.

        Gregg

        Comment

        • John Harrison

          #5
          Re: One Container question


          "Gregg" <gregg@invalid. invalid> wrote in message
          news:Xns94F0A46 AF65Egregginval idinvalid@207.6 9.154.203...[color=blue]
          > "Pat" <Pat@Pat.com> wrote in news:40ad879b$1 _2@rain.i-cable.com:
          >[color=green]
          > > X is double number.[/color]
          >
          > Then just use a std::set<double >[/color]

          And change

          x.push_back(d);

          to

          x.insert(d);

          john


          Comment

          • Jeff Schwab

            #6
            Re: One Container question

            Gregg wrote:[color=blue]
            > "Pat" <Pat@Pat.com> wrote in news:40ad879b$1 _2@rain.i-cable.com:
            >
            >[color=green]
            >>X is double number.[/color]
            >
            >
            > Then just use a std::set<double > to keep track of what you've added to the
            > vector. You might also consider using std::map<int, double> and not use the
            > vector at all.[/color]

            Do you mean std::map<double , int>?

            Comment

            • Paul

              #7
              Re: One Container question

              Gregg wrote:
              [color=blue]
              > "Pat" <Pat@Pat.com> wrote in news:40ad879b$1 _2@rain.i-cable.com:
              >
              >[color=green]
              >>X is double number.[/color]
              >
              >
              > Then just use a std::set<double > to keep track of what you've added to the
              > vector. You might also consider using std::map<int, double> and not use the
              > vector at all. The suitablility of this depends on how often you will be
              > looking up entries versus adding them. Perhaps you don't need to look up by
              > index at all, in which case just use a set and do away with the vector.
              >
              > Gregg[/color]

              The problem with std::set<double > is that double values rarely are
              equal. Tests for equality (as std::set::find( ) does) will yield
              inconsistent results.

              Paul

              Comment

              • Gregg

                #8
                Re: One Container question

                Paul <Paul@Paul.ne t> wrote in
                news:c5f2af53d1 6201ad040272cda bd901c6@news.1u senet.com:
                [color=blue]
                > Gregg wrote:
                >[color=green]
                >> "Pat" <Pat@Pat.com> wrote in news:40ad879b$1 _2@rain.i-cable.com:
                >>
                >>[color=darkred]
                >>>X is double number.[/color]
                >>
                >>
                >> Then just use a std::set<double > to keep track of what you've added
                >> to the vector. You might also consider using std::map<int, double>
                >> and not use the vector at all. The suitablility of this depends on
                >> how often you will be looking up entries versus adding them. Perhaps
                >> you don't need to look up by index at all, in which case just use a
                >> set and do away with the vector.
                >>
                >> Gregg[/color]
                >
                > The problem with std::set<double > is that double values rarely are
                > equal. Tests for equality (as std::set::find( ) does) will yield
                > inconsistent results.
                >
                > Paul
                >[/color]

                I agree in general, but checking for equality was the OP's requirement.
                Depending on how the doubles are being generated (e.g., are they being
                read from a file that contains dollars and cents amounts), this may or
                may not be wise.

                Gregg

                Comment

                • Gregg

                  #9
                  Re: One Container question

                  Jeff Schwab <jeffplus@comca st.net> wrote in
                  news:saSdnToXmu WKejDdRVn-hw@comcast.com:
                  [color=blue]
                  > Gregg wrote:[color=green]
                  >> "Pat" <Pat@Pat.com> wrote in news:40ad879b$1 _2@rain.i-cable.com:
                  >>
                  >>[color=darkred]
                  >>>X is double number.[/color]
                  >>
                  >>
                  >> Then just use a std::set<double > to keep track of what you've added
                  >> to the vector. You might also consider using std::map<int, double>
                  >> and not use the vector at all.[/color]
                  >
                  > Do you mean std::map<double , int>?[/color]

                  No, I meant a map that would enable an int to be supplied as a key to look
                  up a double, so it could serve as a possible substitute for a vector
                  <double>.

                  Gregg

                  Comment

                  Working...