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. update structure in a vector of structures

update structure in a vector of structures

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestiongraphicsdockerannouncement
9 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.
  • C Offline
    C Offline
    csrss
    wrote on last edited by
    #1

    Well, this is kinda maybe wrong name for a question, but... Here is what i need to do (i got no idea how, for now): Here is a structure:

    typedef struct _S
    {
    int a;
    std::string b;
    } S;

    and here is a vector creation:

    std::vector Vec;
    S s;
    s.a = 1;
    s.b = "hi";
    Vec.push_back(s);

    ....

    S s;
    s.a = 10;
    s.b = "something";
    Vec.push_back(s);

    So now there are 10 or something structures in a vector. The question is: I need to be able to update values of any of these structures, while they are still in their positions inside my vector. It is like, for example, i need to update string value in structure which has "a" integer = 10, so in this case in last inserted structure i need to change string value from "something" to, for example, "nothing". How to accomplish such thing? Or maybe i should use something else as a container and not a vector?

    011011010110000101100011011010000110100101101110 0110010101110011

    C P S 3 Replies Last reply
    0
    • C csrss

      Well, this is kinda maybe wrong name for a question, but... Here is what i need to do (i got no idea how, for now): Here is a structure:

      typedef struct _S
      {
      int a;
      std::string b;
      } S;

      and here is a vector creation:

      std::vector Vec;
      S s;
      s.a = 1;
      s.b = "hi";
      Vec.push_back(s);

      ....

      S s;
      s.a = 10;
      s.b = "something";
      Vec.push_back(s);

      So now there are 10 or something structures in a vector. The question is: I need to be able to update values of any of these structures, while they are still in their positions inside my vector. It is like, for example, i need to update string value in structure which has "a" integer = 10, so in this case in last inserted structure i need to change string value from "something" to, for example, "nothing". How to accomplish such thing? Or maybe i should use something else as a container and not a vector?

      011011010110000101100011011010000110100101101110 0110010101110011

      C Offline
      C Offline
      csrss
      wrote on last edited by
      #2

      Ok, i am so stupid =/. Completely forgot about good old iteration with "int i".

      typedef struct _S
      {
      int a;
      std::string str;
      }
      S;

      int main( void)
      {
      std::vector<S> Vec;
      S s;
      s.a = 1; s.str = "hi";
      Vec.push_back(s);
      s.a = 2; s.str = "lol";
      Vec.push_back(s);
      s.a = 3; s.str = "nothing";
      Vec.push_back(s);

      std::vector<S>::iterator it;
      for(it = Vec.begin(); it != Vec.end(); it++)
      {
      	cout << it->a << " : " << it->str.c\_str() << endl;
      }
      
      for(unsigned int i = 0; i < Vec.size(); i++)
      {
      	Vec\[i\].str = "BOOM!";
      }
      
      for(it = Vec.begin(); it != Vec.end(); it++)
      {
      	cout << it->a << " : " << it->str.c\_str() << endl;
      }
      

      }

      011011010110000101100011011010000110100101101110 0110010101110011

      N 1 Reply Last reply
      0
      • C csrss

        Ok, i am so stupid =/. Completely forgot about good old iteration with "int i".

        typedef struct _S
        {
        int a;
        std::string str;
        }
        S;

        int main( void)
        {
        std::vector<S> Vec;
        S s;
        s.a = 1; s.str = "hi";
        Vec.push_back(s);
        s.a = 2; s.str = "lol";
        Vec.push_back(s);
        s.a = 3; s.str = "nothing";
        Vec.push_back(s);

        std::vector<S>::iterator it;
        for(it = Vec.begin(); it != Vec.end(); it++)
        {
        	cout << it->a << " : " << it->str.c\_str() << endl;
        }
        
        for(unsigned int i = 0; i < Vec.size(); i++)
        {
        	Vec\[i\].str = "BOOM!";
        }
        
        for(it = Vec.begin(); it != Vec.end(); it++)
        {
        	cout << it->a << " : " << it->str.c\_str() << endl;
        }
        

        }

        011011010110000101100011011010000110100101101110 0110010101110011

        N Offline
        N Offline
        Niklas L
        wrote on last edited by
        #3

        You can still use iterators

        std::vector<S>::iterator it;
        for(it = Vec.begin(); it != Vec.end(); it++)
        {
        cout << "Before: "<< it->a << " : " << it->str.c_str() << endl;
        it->str = "New string value";
        cout << "After: "<< it->a << " : " << it->str.c_str() << endl;
        }

        The iterator can be thought of as a pointer to your elements. There is no need to use indexing syntax just to update your data.

        home

        C 1 Reply Last reply
        0
        • C csrss

          Well, this is kinda maybe wrong name for a question, but... Here is what i need to do (i got no idea how, for now): Here is a structure:

          typedef struct _S
          {
          int a;
          std::string b;
          } S;

          and here is a vector creation:

          std::vector Vec;
          S s;
          s.a = 1;
          s.b = "hi";
          Vec.push_back(s);

          ....

          S s;
          s.a = 10;
          s.b = "something";
          Vec.push_back(s);

          So now there are 10 or something structures in a vector. The question is: I need to be able to update values of any of these structures, while they are still in their positions inside my vector. It is like, for example, i need to update string value in structure which has "a" integer = 10, so in this case in last inserted structure i need to change string value from "something" to, for example, "nothing". How to accomplish such thing? Or maybe i should use something else as a container and not a vector?

          011011010110000101100011011010000110100101101110 0110010101110011

          P Offline
          P Offline
          Parthi_Appu
          wrote on last edited by
          #4

          Hi, If the interger values are going to be unique then you can go for,

          std::map<int, struct S>


          Do your Duty and Don't expect the Result

          C 1 Reply Last reply
          0
          • N Niklas L

            You can still use iterators

            std::vector<S>::iterator it;
            for(it = Vec.begin(); it != Vec.end(); it++)
            {
            cout << "Before: "<< it->a << " : " << it->str.c_str() << endl;
            it->str = "New string value";
            cout << "After: "<< it->a << " : " << it->str.c_str() << endl;
            }

            The iterator can be thought of as a pointer to your elements. There is no need to use indexing syntax just to update your data.

            home

            C Offline
            C Offline
            csrss
            wrote on last edited by
            #5

            Thanks, thats correct. I am just still thinking in C :)

            011011010110000101100011011010000110100101101110 0110010101110011

            1 Reply Last reply
            0
            • P Parthi_Appu

              Hi, If the interger values are going to be unique then you can go for,

              std::map<int, struct S>


              Do your Duty and Don't expect the Result

              C Offline
              C Offline
              csrss
              wrote on last edited by
              #6

              Thanks, i'll check this one out

              011011010110000101100011011010000110100101101110 0110010101110011

              1 Reply Last reply
              0
              • C csrss

                Well, this is kinda maybe wrong name for a question, but... Here is what i need to do (i got no idea how, for now): Here is a structure:

                typedef struct _S
                {
                int a;
                std::string b;
                } S;

                and here is a vector creation:

                std::vector Vec;
                S s;
                s.a = 1;
                s.b = "hi";
                Vec.push_back(s);

                ....

                S s;
                s.a = 10;
                s.b = "something";
                Vec.push_back(s);

                So now there are 10 or something structures in a vector. The question is: I need to be able to update values of any of these structures, while they are still in their positions inside my vector. It is like, for example, i need to update string value in structure which has "a" integer = 10, so in this case in last inserted structure i need to change string value from "something" to, for example, "nothing". How to accomplish such thing? Or maybe i should use something else as a container and not a vector?

                011011010110000101100011011010000110100101101110 0110010101110011

                S Offline
                S Offline
                Stefan_Lang
                wrote on last edited by
                #7

                As an aside, you don't need a typedef with every struct definition - you can use the 'tag' of the struct in the same way you can use a class name. Two options: 1. If you really only have an int that serves as soem sort of unique ID, and some value associated to that ID, then you should use a map rather than a vector. std::map automatically sorts your elements by your key values when you insert them. Retrieving an item is a simple and very fast operation. 2. If there's more to it, you can still use the std::find() method to locate a specific struct in your vector. You only need to define an equality operator for your struct like this: (not tested)

                #include <vector> // std::vector
                #include <algorithm> //std::find()
                struct S {
                int a;
                std::string b;
                bool operator==(const S& s) const {
                return s.a==a;
                }
                S(int a_) : a(a_) {} // constructor, just for convenience
                S(int a_, std::string b_) : a(a_), b(b_) {} // constructor, even more convenient
                }

                int main() {
                // ...
                std::vector s;
                s.push_back(S(1, "hi"));
                // ...
                s.push_back(S(10, "hello"));

                std::vector::iterator location = std::find(s.begin(), s.end(), 5); // finds element with a==5
                std::string result;
                if (location != s.end(); // found something?
                result = location->b;
                }

                You can check the online documentation on std::find() and std::find_if() for examples. P.S.: anyone knows what is messing with code coloring here? :confused:

                modified on Friday, April 8, 2011 2:40 AM

                C 1 Reply Last reply
                0
                • S Stefan_Lang

                  As an aside, you don't need a typedef with every struct definition - you can use the 'tag' of the struct in the same way you can use a class name. Two options: 1. If you really only have an int that serves as soem sort of unique ID, and some value associated to that ID, then you should use a map rather than a vector. std::map automatically sorts your elements by your key values when you insert them. Retrieving an item is a simple and very fast operation. 2. If there's more to it, you can still use the std::find() method to locate a specific struct in your vector. You only need to define an equality operator for your struct like this: (not tested)

                  #include <vector> // std::vector
                  #include <algorithm> //std::find()
                  struct S {
                  int a;
                  std::string b;
                  bool operator==(const S& s) const {
                  return s.a==a;
                  }
                  S(int a_) : a(a_) {} // constructor, just for convenience
                  S(int a_, std::string b_) : a(a_), b(b_) {} // constructor, even more convenient
                  }

                  int main() {
                  // ...
                  std::vector s;
                  s.push_back(S(1, "hi"));
                  // ...
                  s.push_back(S(10, "hello"));

                  std::vector::iterator location = std::find(s.begin(), s.end(), 5); // finds element with a==5
                  std::string result;
                  if (location != s.end(); // found something?
                  result = location->b;
                  }

                  You can check the online documentation on std::find() and std::find_if() for examples. P.S.: anyone knows what is messing with code coloring here? :confused:

                  modified on Friday, April 8, 2011 2:40 AM

                  C Offline
                  C Offline
                  csrss
                  wrote on last edited by
                  #8

                  Hey thanks! This gave me some ideas :) P.s. i know that i dont need typedefs for structs ;) btw. i had some time ago same issue with coloring thing - that is because you haven't probably replace html tags (<>)

                  011011010110000101100011011010000110100101101110 0110010101110011

                  S 1 Reply Last reply
                  0
                  • C csrss

                    Hey thanks! This gave me some ideas :) P.s. i know that i dont need typedefs for structs ;) btw. i had some time ago same issue with coloring thing - that is because you haven't probably replace html tags (<>)

                    011011010110000101100011011010000110100101101110 0110010101110011

                    S Offline
                    S Offline
                    Stefan_Lang
                    wrote on last edited by
                    #9

                    Glad to be of help.

                    csrss wrote:

                    you haven't probably replace html tags

                    Good call! After replacing the < and > in the #include lines it worked. It's strange though, inside the actual code this wasn't neccesary.

                    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