473,387 Members | 1,534 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Is there a way to have 2 vectors mix together?

I want to add 2 vectors to print out so that there on the same line. What I am trying to make is an inventory system that will use 2 vectors to keep the pounds of the item and list the 2 vectors on one line.

(I am using Microsoft Visual C++ 2010 Express)

Like this:

0. empty 0
1. empty 0
2. empty 0

ect...

Right now it looks like this:

0. empty
0. 0

The code:

Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <vector>
  4. #include <string>
  5. #include <fstream>
  6. #include <cmath>
  7.  
  8. using namespace std;
  9.  
  10. int main()
  11. {
  12.     vector<string> inv;
  13.     vector<int> pou;
  14.     int num;
  15.     int del;
  16.     int asdf;
  17.     string name;
  18.  
  19.     inv.push_back("Empty");
  20.     pou.push_back(0);
  21.  
  22.     vector<string>::const_iterator iter;
  23.     vector<int>::const_iterator iter2;
  24.     vector<string>::iterator myIterator;
  25.  
  26.     cout << "\t\t\t\t*Inventory Wight*\n\n";
  27.     cout << "Your inventory and pounds:\n";
  28.  
  29.     asdf = -1;
  30.         for (iter = inv.begin(); iter != inv.end(); ++iter)
  31.         {
  32.             ++asdf;
  33.             cout << asdf << ". " << *iter << endl;
  34.         }
  35.     asdf = -1;
  36.         for (iter2 = pou.begin(); iter2 != pou.end(); ++iter2)
  37.         {
  38.             ++asdf;
  39.             cout << asdf << ". " << *iter2 << endl;
  40.         }
  41.  
  42. system("pause");
  43.     return 0;
  44. }

I am new, I use this type of code because its the code i know the best to use in C++ in my class.
Mar 27 '13 #1

✓ answered by weaknessforcats

After you do the push_back of obj, the data is now in the vector leaving obj able to be reused. So just change the values in obj and do a second push_back. Both sets of values will now be in the vector.

Your loop that starts with begin() and runs to end() will cycle twice and you should see both sets of values.

14 1529
Rabbit
12,516 Expert Mod 8TB
I'm assuming each vector is the same size? Move the cout line from the second loop into the first loop and get rid of the second loop.
Mar 27 '13 #2
weaknessforcats
9,208 Expert Mod 8TB
I suggest you use only one vector.

Each inventory item has a name and a number of pounds so your design should imitate reality. In this case I would have a struct named InventoryItem and the struct members would be a string for the name and a int for the number of pounds.

Then you only have a vector<InventoryItem>.

With two vectors, it's too difficult to make sure the string in the 3rd element stays synched with the int in the 3rd element in the other vector. Especially since vectors can move items around without telling you about it.
Mar 27 '13 #3
How could I have one vector that would imitate a string and int? I my not be following correctly but do you want me to have both pounds and the item inside the vector? Since the vector can move my items around with out notice how would that help?
Mar 27 '13 #4
I an only 5 weeks trained in C++ and do not understand how to put a string and int together is there a way to do so???
Mar 28 '13 #5
weaknessforcats
9,208 Expert Mod 8TB
You are allowed to group related data items under one name. This is done using a struct or class. These two syntaxes are essentially the same and the difference at 5 weeks into C++ is not important.

You can do this:

Expand|Select|Wrap|Line Numbers
  1. struct InventoryItem
  2. {
  3.    string name;
  4.    int  weight;
  5. };
Then you create InventoryItem variables:

Expand|Select|Wrap|Line Numbers
  1. InventoryItem  obj;
  2.  
Now you can add values to the struct members:
Expand|Select|Wrap|Line Numbers
  1. obj.name = "flour";
  2. obj.weight = 100;
  3.  
Then you can place the object in a vector:

Expand|Select|Wrap|Line Numbers
  1. struct InventoryItem
  2. {
  3.    string name;
  4.    int  weight;
  5. };
  6.  
  7. int main()
  8. {
  9. vector<InventoryItem> MyStuff;
  10.  
  11. InventoryItem  obj;
  12. obj.name = "flour";
  13. obj.weight = 100;
  14.  
  15. MyStuff.push_back(obj);  //add item to inventory
Since a copy of obj is tucked away in the vector, you can now use obj for the next inventory item.
Mar 28 '13 #6
Thank you sir, I have learned a lot while reading this that are teacher does not want to bother with until 2nd year. I think I can fully build on this to make my low class texted based game.
Mar 28 '13 #7
There seems to be a small problem, the obj does not like being named as:

InventoryItem obj;
obj.name = "flour";
obj.weight = 100;

Also is there something wrong with this code? I tried to adjust it to yours weakness, but I feel like i am doing something wrong.

Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <vector>
  4. #include <string>
  5. #include <fstream>
  6. #include <cmath>
  7.  
  8. using namespace std;
  9.  
  10.  
  11.     struct InventoryItem;
  12. {
  13.     string name;
  14.     int weight;
  15. };
  16.  
  17. int main()
  18. {
  19.  
  20.     vector<InventoryItem> MyStuff;
  21.  
  22.     InventoryItem obj;
  23.     obj.name = "flour";
  24.     obj.weight = 100;
  25.     int num;
  26.     int del;
  27.     int asdf;
  28.     string name;
  29.  
  30.     MyStuff.push_back(obj); //add item to inventory
  31.  
  32.     vector<InventoryItem>::const_iterator iter;
  33.     vector<InventoryItem>::iterator myIterator;
  34.  
  35.     cout << "\t\t\t\t*Inventory Wight*\n\n";
  36.     cout << "Your inventory and pounds:\n";
  37.  
  38.     asdf = -1;
  39.         for (iter = MyStuff.begin(); iter != MyStuff.end(); ++iter)
  40.         {
  41.             ++asdf;
  42.             cout << asdf << ". " << *iter << endl;
  43.         }
  44.  
  45. system("pause");
  46.     return 0;
  47. }
Mar 28 '13 #8
Sorry rabbit am new thought they just appeared... Little bit dumb on my part. ;)
Mar 28 '13 #9
weaknessforcats
9,208 Expert Mod 8TB
This code:

Expand|Select|Wrap|Line Numbers
  1.    struct InventoryItem;   <<<<---!!!
  2.   {
  3.       string name;
  4.       int weight;
  5.   };
  6.  
has a semi-colon that needs to be removed.

There is no problem assigning "flour" to the string.

However, when you display you need to remember that *iter is an InventoryItem and the compiler gags because it does not now how to display a user-defined object. ater you will learn how to do this. For now, you need to display each indivdual member:

Expand|Select|Wrap|Line Numbers
  1. cout << (*iter).name << "\n" << (*iter).weight;
Once I removed that semi-colon and changed the cout to display each member your program took off and ran like a bunny.
Mar 29 '13 #10
Thank you sir, now I will be able to make a game for my class with the great way to make the inventory system, I take my hat off to you good sir.
Mar 29 '13 #11
Now! There is one final thing before I stop this forum post. I have the code, working just like i need. But i have tried to add 2 objects, one is wool and one is flour. The wool's weight is 50 and the flour is 100 I am trying to make it work but that is my only problem right now, i can mix and use one objects numbers but combine it to have 2 or more.

I want to have it so it can spit out:

0. flour Weight: 100
1. wool Weight: 50

I am sorry to ask more questions this is stuff I am still trying to grasp.
Mar 29 '13 #12
weaknessforcats
9,208 Expert Mod 8TB
After you do the push_back of obj, the data is now in the vector leaving obj able to be reused. So just change the values in obj and do a second push_back. Both sets of values will now be in the vector.

Your loop that starts with begin() and runs to end() will cycle twice and you should see both sets of values.
Mar 29 '13 #13
thanks, works great now!

I love you man.

Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <vector>
  4. #include <string>
  5. #include <fstream>
  6. #include <cmath>
  7.  
  8. using namespace std;
  9.  
  10.  
  11.     struct InventoryItem
  12. {
  13.     string name;
  14.     int weight;
  15. };
  16.  
  17. int main()
  18. {
  19.  
  20.     vector<InventoryItem> MyStuff;
  21.  
  22.     InventoryItem obj;
  23.     InventoryItem obj2;
  24.     obj.name = "flour";
  25.     obj.weight = 100;
  26.     obj2.name = "wool";
  27.     obj2.weight = 50;
  28.     int num;
  29.     int del;
  30.     int asdf;
  31.     string name;
  32.  
  33.     MyStuff.push_back(obj); //add item to inventory
  34.     MyStuff.push_back(obj2);
  35.  
  36.     vector<InventoryItem>::const_iterator iter;
  37.     vector<InventoryItem>::iterator myIterator;
  38.  
  39.     cout << "\t\t\t\t*Inventory Wight*\n\n";
  40.     cout << "Your inventory and pounds:\n";
  41.  
  42.     asdf = -1;
  43.         for (iter = MyStuff.begin(); iter != MyStuff.end(); ++iter)
  44.         {
  45.             ++asdf;
  46.             cout << asdf << ". " << (*iter).name << " Weight: " << (*iter).weight << "\n";
  47.         }
  48.  
  49. system("pause");
  50.     return 0;
  51. }
Mar 29 '13 #14
Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <vector>
  4. #include <string>
  5. #include <fstream>
  6. #include <cmath>
  7.  
  8. using namespace std;
  9.  
  10.     struct InventoryItem
  11. {
  12.     string name;
  13.     int weight;
  14.     int price;
  15. };
  16.  
  17. int main()
  18. {
  19.  
  20.     vector<InventoryItem> MyStuff;
  21.  
  22.     //Item 1
  23.     InventoryItem obj;
  24.     obj.name = "flour";
  25.     obj.weight = 100;
  26.     obj.price = 10;
  27.     //Item 2
  28.     InventoryItem obj2;
  29.     obj2.name = "wool";
  30.     obj2.weight = 50;
  31.     obj2.price = 20;
  32.     //Item 3
  33.     InventoryItem obj3;
  34.     obj3.name = "sword";
  35.     obj3.weight = 150;
  36.     obj3.price = 50;
  37.  
  38.  
  39.     int num;
  40.     int del;
  41.     int asdf;
  42.     string name;
  43.  
  44.     MyStuff.push_back(obj); //add item 1 to inventory
  45.     MyStuff.push_back(obj2); //add item 2 to inventory
  46.     MyStuff.push_back(obj3); //add item 3 to inventory
  47.  
  48.     vector<InventoryItem>::const_iterator iter;
  49.     vector<InventoryItem>::iterator myIterator;
  50.  
  51.     cout << "\t\t\t\t*Inventory Wight*\n\n";
  52.     cout << "Your inventory and pounds:\n";
  53.  
  54.     asdf = 0;
  55.         for (iter = MyStuff.begin(); iter != MyStuff.end(); ++iter)
  56.         {
  57.             ++asdf;
  58.             cout << asdf << ". " << (*iter).name << " | Weight: " << (*iter).weight << " | Price: " << (*iter).price << "\n";
  59.         }
  60.  
  61. system("pause");
  62.     return 0;
  63. }
Mar 29 '13 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Pratyush | last post by:
Hi, Suppose there is a vector of objects of class A, i.e., std::vector<A> vec_A(N); The class A satisifies all the STL vector requirements. Now I wish to add some attributes for each of the...
5
by: Computer Whizz | last post by:
I was reading through Accelerated C++ at work when I read through the first mention of Vectors, giving us certain functions etc. Is there any benefit of Arrays over Vectors? Since all Vectors...
7
by: john townsley | last post by:
when using vectors with pointers of objects. is it best to create instances then point to them or dynamic allocation upon creating a vector element. I can do this, but is creating a vector with...
3
by: Amit | last post by:
Hello. I am having some problem organizing a set of vectors. The vectors itself, could contain a pointer( say integer pointer) or could contain another object MyClass. 1>So, first of all, is...
4
by: Dr. J.K. Becker | last post by:
Hi all, I have vectors that holds pointers to other vectors, like so: vector<whatever> x; vector<whatever*> z; z=&x; Now I add something to x
13
by: JoeC | last post by:
I am completly lost. I would like to create a function that takes two vectors. These two vectors have objects with x and y coords, what I want to do find all the objects in the same x and y...
5
by: madhu | last post by:
http://msdn2.microsoft.com/en-us/library/fs5a18ce(VS.80).aspx vector <intv1; v1.push_back( 10 ); //adds 10 to the tail v1.push_back( 20 ); //adds 20 to the tail cout << "The size of v1 is " <<...
11
by: Chris Roth | last post by:
vector<doublev1; vector<doublev2; What is the best way to add v1 to v2? (v2 = v1+v2 that is)? Simply iterate through, or can an algorithm like transform be used? I have used transform to add a...
1
by: Rob | last post by:
How would I do this? I want to be able to handle vectors of many different types of data and vectors that can contain any number of other vectors of data. Currently, I have a templated...
17
by: fgh.vbn.rty | last post by:
I know that std::vector<boolis specialized to store individual bools as single bits. I have a question about the comparison operation on a pair of vector<bool>s. Is the comparison done bit by...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.