9 Replies - 542 Views - Last Post: 02 December 2017 - 03:03 AM Rate Topic: -----

#1 Failing Calculus   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 07-March 16

User entry to determine which vector to use in an operation

Posted 30 November 2017 - 05:10 PM

Hello , I have a function that takes in two parameters in the form of vector<int>'s. I have 7 of these vectors total, and want to let the user determine which vectors are used in the function. This is what I have so far -

		cout << "What Pile would you like to Move from? | 1 , 2 , 3 , 4 , 5 , 6 , 7" << endl;
		cin >> pileChoice1;
		cout << pileChoice1;
		if (pileChoice1 == '1') { moveVector1 = handVec1; }
		else if (pileChoice1 == '2') { moveVector1 = handVec2; }
		else if (pileChoice1 == '3') { moveVector1 = handVec3; }
		else if (pileChoice1 == '4') { moveVector1 = handVec4; }
		else if (pileChoice1 == '5') { moveVector1 = handVec5; }
		else if (pileChoice1 == '6') { moveVector1 = handVec6; }
		else if (pileChoice1 == '7') { moveVector1 = handVec7; }
		cout << "Moving from : " << pileChoice1 << endl;
		
		cout << "What pile would you like to move to?" << endl;
		cin >> pileChoice2;
		if (pileChoice2 == '1') { moveVector2 = handVec1; }
		else if (pileChoice2 == '2') { moveVector2 = handVec2; }
		else if (pileChoice2 == '3') { moveVector2 = handVec3; }
		else if (pileChoice2 == '4') { moveVector2 = handVec4; }
		else if (pileChoice2 == '5') { moveVector2 = handVec5; }
		else if (pileChoice2 == '6') { moveVector2 = handVec6; }
		else if (pileChoice2 == '7') { moveVector2 = handVec7; }
		deck.moveCard(moveVector1, moveVector2);


Output-
What Pile would you like to Move from? | 1 , 2 , 3 , 4 , 5 , 6 , 7
1
Moving from : 1  Testing moveVector 1 : 7
What pile would you like to move to?
2
Moving to : 2
Testing pilechoice2 : 2  Testing moveVector2: 8


As it is, the function does absolutely nothing. But I get the desired result if I simply put in handVec1 and handVec2 into the function rather than moveVector. Also in the lines where I cout the moveVectors, they contain the correct values. pileChoice is initialized as a char, it can be an int if that would change anything.

This post has been edited by Failing Calculus: 30 November 2017 - 05:12 PM


Is This A Good Question/Topic? 0
  • +

Replies To: User entry to determine which vector to use in an operation

#2 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6166
  • View blog
  • Posts: 21,264
  • Joined: 05-May 12

Re: User entry to determine which vector to use in an operation

Posted 30 November 2017 - 06:29 PM

First to answer your question: The reason is that when you assign one vector to another a copy is made. You likely were looking as some Java or C# code where references are assigned instead of values being copied.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6166
  • View blog
  • Posts: 21,264
  • Joined: 05-May 12

Re: User entry to determine which vector to use in an operation

Posted 30 November 2017 - 06:34 PM

Now to address a design issue: Why not simply have an array of vectors or a vector of vectors. Then you can simply do:
deck.moveCard(handVec[pileChoice1 - '1'], handVec[pileChoice2 - '1']);


Was This Post Helpful? 0
  • +
  • -

#4 Failing Calculus   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 07-March 16

Re: User entry to determine which vector to use in an operation

Posted 30 November 2017 - 06:46 PM

In your example above, wouldn't that be getting a position in the same handVec? It looks like its just using pileChoice as a way to specify which element in the vector to use. I need to be able to let the user pick between vectors named handvec1, handvec2, and so on.
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3786
  • View blog
  • Posts: 13,715
  • Joined: 08-August 08

Re: User entry to determine which vector to use in an operation

Posted 30 November 2017 - 07:07 PM

Maybe this will make it clearer.
#include <iostream>
#include <vector>

using namespace std;

int main(int argc, const char * argv[]) {
	vector<int> tmp;
	vector< vector<int> > vecs(7,tmp);
	int num = 1;
	for(size_t i = 0; i < vecs.size(); i++) {
		do {
			vecs[i].push_back(num);
			num++;
		} while(num % 10 != 0);
	}

	cout <<"Vector of vectors:" << endl;
	for(size_t i = 0; i < vecs.size(); i++) {
		cout << i <<": ";
		for(size_t j = 0; j < vecs[i].size(); j++) {
			cout << vecs[i][j] << " ";
		}
		cout << endl;
	}

	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#6 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6166
  • View blog
  • Posts: 21,264
  • Joined: 05-May 12

Re: User entry to determine which vector to use in an operation

Posted 30 November 2017 - 07:07 PM

As I said, use an array of vectors, or a vector of vectors.

Instead of having
vector<Card> handVec1;
vector<Card> handVec2;
vector<Card> handVec3;
vector<Card> handVec4;
vector<Card> handVec5;
vector<Card> handVec6;
vector<Card> handVec7;



you have:
vector<Card> handVec[7];



or:
vector<vector<Card>> handVec;


Was This Post Helpful? 0
  • +
  • -

#7 Failing Calculus   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 07-March 16

Re: User entry to determine which vector to use in an operation

Posted 01 December 2017 - 06:51 AM

To be completely honest I had no idea that was possible. I have to backtrack to make that work but you are right, that is a far beftter way of doing things.
Was This Post Helpful? 0
  • +
  • -

#8 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 801
  • View blog
  • Posts: 3,264
  • Joined: 13-June 14

Re: User entry to determine which vector to use in an operation

Posted 01 December 2017 - 02:04 PM

View PostFailing Calculus, on 01 December 2017 - 01:51 PM, said:

To be completely honest I had no idea that was possible.


Out of interest, how come? Just like a Card is a thing, a vector is a thing and you often want collections of things. Just because a vector is itself a collection, that doesn't stop you asking for collections of them! In the same way, you might consider a matrix to be a collection of rows, where a row is a collection of elements.

This post has been edited by ndc85430: 01 December 2017 - 02:10 PM

Was This Post Helpful? 0
  • +
  • -

#9 Failing Calculus   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 07-March 16

Re: User entry to determine which vector to use in an operation

Posted 01 December 2017 - 03:04 PM

I suppose it's because I wasn't viewing a vector like that, but the comparison to a matrix is actually really helpful. I was kind of viewing each element of a vector as a single number, character, or string. That way of thinking really limits your possibilities though. So a vector is basically a type of container right? And it's possible to place other containers inside that container, as long as they have the same data type? Kind of like Russian dolls?
Was This Post Helpful? 0
  • +
  • -

#10 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 801
  • View blog
  • Posts: 3,264
  • Joined: 13-June 14

Re: User entry to determine which vector to use in an operation

Posted 02 December 2017 - 03:03 AM

View PostFailing Calculus, on 01 December 2017 - 10:04 PM, said:

So a vector is basically a type of container right?


Yes. Other kinds of containers in the C++ standard library include std::map and std::stack. See, e.g. this for a full list (under "Containers library").

Quote

And it's possible to place other containers inside that container, as long as they have the same data type?


Yes. If you understand that, say, a std::vector<int> can only hold ints, then naturally, you can understand that a std::vector<std::vector<Card>> can only hold std::vector<Card>s.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1