error C2679: binary '<<': no operator found which takes a right-hand operand of type 'std::vector<int,std::allocator<_Ty>>'

Sep 6, 2019 at 3:24pm
Hi everyone, I'm new to C++. I've been assigned the following tasks.
1. Choose any two random numbers in the range 3 to 9.
2. Find position of these random numbers for each row.
3. Swap these two random numbers for each row.
4. Display new vector after swapping.

I tried to solve it but error occurred.
Error:
error C2679: binary '<<': no operator found which takes a right-hand operand of type 'std::vector<int,std::allocator<_Ty>>' (or there is no acceptable conversion)

I don't know how to fix it. Hopefully someone can help me. Thank you :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>     // cout
#include <ctime>		// time
#include <cstdlib>		// srand, rand()
#include <algorithm>    // find(), iter_swap()
#include <vector>       // vector

using namespace std;

int main()
{
	// Initialize random seed:
	srand(time(NULL));

	vector< vector<int> > vec =
	{
		{ 9, 8, 7, 6, 5, 4, 3, 2, 1 },
		{ 1, 2, 3, 4, 5, 6, 7, 8, 9 },
		{ 5, 4, 3, 2, 1, 9, 8, 7, 6 }
	};

	// Print Original Vector
	cout << "Original vector :" << endl;
	for (size_t i = 0; i < vec.size(); i++)
	{
		for (size_t j = 0; j < vec[i].size(); j++)
		{
			cout << vec[i][j] << " ";
		}
		cout << "\n";
	}
	
	// Generate two random number in the range 3 to 9
	int r1, r2;
	do {
		r1 = rand() % 9 + 3;      // r1 in the range 3 to 9
		r2 = rand() % 9 + 3;	// r2 in the range 3 to 9
	} while (r1 == r2);

	cout << "First random number = " << r1 << endl;
	cout << "Second random number = " << r2 << endl;

	// Iterator used to store the position of searched element
	

	for (size_t i = 0; i < vec.size(); i++)
	{
		vector <vector<int> >::iterator it, ik;

		// Find position of first random number, r1
		it = find(vec.begin(), vec.end(), r1);
		if (it != vec.end())
		{
			cout << "First random number " << r1 << " was found at position ";
			cout << it - vec.begin() + 1 << "\n";
		}
		else
			cout << "Number not found.\n\n";

		// Find position of second random number, r2
		ik = find(vec.begin(), vec.end(), r2);
		if (ik != vec.end())
		{
			cout << "Second random number " << r2 << " was found at position ";
			cout << ik - vec.begin() + 1 << "\n";
		}
		else
			cout << "Number not found.\n\n";

		// Swap them
		if (it != vec.end() && ik != vec.end())
		{
			cout << "Swapping random numbers " << *it << " and " << *ik << '\n';
			iter_swap(it, ik);
		}

		// Print New Vector
		cout << "New vector :";
		for (size_t i = 0; i < vec.size(); i++)
		{
			for (size_t j = 0; j < vec[i].size(); j++)
			{
				cout << vec[i][j] << " ";
			}
			cout << "\n";
		}
	}

	return 0;
}
Sep 6, 2019 at 3:47pm
Here:
 
cout << "Swapping random numbers " << *it << " and " << *ik << '\n';

The type of it is std::vector<std::vector<int>>::iterator, so the type of *it is std::vector<int>.

Are you sure the rest of the code in the loop does what you think it does? What it really does is not swap two numbers. It's swapping two vectors; two rows, if you will.
Sep 6, 2019 at 3:55pm
Because you have a vector of vectors, your iterators are pointing to an entire vector of values, not a single value ... so the << operator won't know how to output them.

Perhaps you would like to explain exactly how you think "your" code works?

I think it would be easier to advise if you gave us VERBATIM the bit of your assignment BEFORE what you have quoted: we are only getting a very confused story and it is unclear what you are being asked.
Sep 6, 2019 at 4:34pm
HiHelios,

Are you sure the rest of the code in the loop does what you think it does? What it really does is not swap two numbers. It's swapping two vectors; two rows, if you will.

Can't I use iter_swap() function to swap numbers?

Hilastchance,

I think it would be easier to advise if you gave us VERBATIM the bit of your assignment BEFORE what you have quoted: we are only getting a very confused story and it is unclear what you are being asked.


Sorry for inconvenience. I was given a task to generate two random numbers between 3 to 9.
For example: first random number = 5 and second random number = 9.

Then, I need to find the position of these random number for each row vector as follow:
{ 9, 8, 7, 6, 5, 4, 3, 2, 1 } --> first random number = position 5, second random number = position 1
{ 1, 2, 3, 4, 5, 6, 7, 8, 9 } --> first random number = position 5, second random number = position 9
{ 5, 4, 3, 2, 1, 9, 8, 7, 6 } --> first random number = position 1, second random number = position 6.

Then, swap these random numbers for each row
{ 9, 8, 7, 6, 5, 4, 3, 2, 1 } --> { 5, 8, 7, 6, 9, 4, 3, 2, 1 }
{ 1, 2, 3, 4, 5, 6, 7, 8, 9 } --> { 1, 2, 3, 4, 9, 6, 7, 8, 5 }
{ 5, 4, 3, 2, 1, 9, 8, 7, 6 } --> { 9, 4, 3, 2, 1, 5, 8, 7, 6 }
Sep 6, 2019 at 4:45pm
You need to deal with each vec[i] in turn, not the whole of vec.

I presume you know whether the number refers to the value or its position.
Last edited on Sep 6, 2019 at 4:46pm
Sep 6, 2019 at 5:00pm
Hi lastchance,

You need to deal with each vec[i] in turn, not the whole of vec.

Does it mean I need to deal with each column not each row.

Sorry for asking. A bit confusing..
Sep 6, 2019 at 5:33pm
Does it mean I need to deal with each column not each row.
You need to deal with both.
Sep 6, 2019 at 6:12pm
sarah1993 wrote:
Does it mean I need to deal with each column not each row.

You need to deal with the i'th row (that, is, vec[i]) inside the i loop.

1
2
3
		vector <vector<int> >::iterator it, ik;
		// Find position of first random number, r1
		it = find(vec.begin(), vec.end(), r1);

needs to be
1
2
3
		vector<int>::iterator it, ik;
		// Find position of first random number, r1
		it = find(vec[i].begin(), vec[i].end(), r1);

with a whole mass of other changes from vec to vec[i] below it.


For your final output either move it outside the i loop, or just do one row vector at a time.

Your random numbers are currently in the range 3 to 11, not 3 to 9.
Last edited on Sep 6, 2019 at 6:20pm
Sep 7, 2019 at 3:58am
Hi lastchance and helios, thank you for your advise.

I've improved this codes based on your explanation and it worked. Thank you so much for helping. I really appreciated it.
Topic archived. No new replies allowed.