c++ - Adding N elements from user to an vector endline -


I have trouble using a vector for adding elements at the end of the line.

My Stretch:

  Structure Process_s {int type_id; Int line_id; Int product_id; };  

Then I change it using a vector:

  std :: vector & lt; Process_s> Process_v;  

My next step is populating two rows:

  process_v.at (1) .type_id = 1; Process_v.at (1) .line_id = 1; Process_v.at (1) .product_id = 2;  

and

  process_v.at (2) .type_id = 1; Process_v.at (2) .line_id = 2; Process_v.at (2) .product_id = 2;  

From now on, my goal gets all the user input and connects to the last (1) or second line (2) in the end, all data is available on time, I get it with ODD and EVEN , Or in other words, I will copy the line one (1) to the three (3) line and copy line two (2) line (4), the number

the first problem The attempt to add more data than user input from first or second row to end Is trying to use this:

  process .at (1) = 1  

or

  process (1) .push_back = 1  

From this point I need to store user input on the respective line, once the user terminates the input, So type a TXT like this:

The specification is for type (type_id) and line (line_id), first product is needed (product_id), second (product_id), etc. ...

of these No one is working, someone will tell me what am I missing?

Thanks in the advice

By default, when you create a vector Is empty, so that you "first" or "second" element in vector.

If you know that your vector has 2 elements, then you can start it with the size of 2:

  std :: vector & lt; Process_s> Process_v (2);  

Then you can populate it:

  process_v.at (0) .type_id = 1; Process_v.at (0) .line_id = 1; Process_v.at (0) .product_id = 2; Process_v.at (1) .type_id = 1; Process_v.at (1) .line_id = 2; Process_v.at (1) .product_id = 2;  

Note that vectors are zero-based C ++ so that you get the first line to process_v.at (0) (or process_v [0] )

If you do not know how big your vector should be, then use process_s to add your vector to push_back Should:

  std :: vector & lt; Process_s> Process_v; Process_s p; P.type_id = 1; P.line_id = 1; P.product_id = 2; Process_v.push_back (p);  

or

  process_v.push_back (process_s {1,1,2});  

or

  process_v.push_back ({1,1,2});  

Comments

Popular posts from this blog

apache - 504 Gateway Time-out The server didn't respond in time. How to fix it? -

c# - .net WebSocket: CloseOutputAsync vs CloseAsync -

c++ - How to properly scale qgroupbox title with stylesheet for high resolution display? -