problem with "<<"

although y and the vectorm temp is defined as floats it gives an error about this line.
y >> temp.at(j) ;

error says:
left operand has type 'float'
right operand has type 'float'
Your thread subject says << but your text says >>

But either way you cannot use the bitshift operators << or >> on float types.
Last edited on
is there another way i send floats to vector?
You can't "send" floats to a vector (are you thinking of cin >> usage here?). But, if 'y' is some random float and 'temp' is a vector of floats, you can set the value of an element

temp.at(j) = y; // set the element at index k to y

or

temp[j] = y;

(here, the vector must have a size of at least j + 1)

or append an element to the back of the vector

temp.push_back(y); // add y to the end of the vector

(push_back increases the size by 1)

For other things you can do with vector, see (e.g. insert)
http://www.cplusplus.com/reference/stl/vector/

Andy

PS But you can read a float from cin (or from a file using ifstream, etc) into a vector element if the vector is big enough.

1
2
3
4
vector<float> temp(10); // vector starts off with 10 zeroed elements

cin >> temp.at(2); // ok (2 is less than 10)
cin >> temp[3];    // also ok 


operator>>, in this case the extraction operator, is intended for use with input streams. And as guestgulkan has already said, operator>>, as the right bit shift operator (e.g.
m = m >> 3; // shift m 3 bits to the right
) only works with integers.
Last edited on
thank you so much but still have a problem. the float variable y is read from a file and the code i wrote is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
float y;
 ifstream myfile ("example.txt");
 /* for(int j=0;j<3;j++){
			myfile>>y;
			cout << y<< "\n";	
			for un içine vector<float>::size_type  }*/
	vector<float> temp(2500);
	vector<float>press(2500);
	std::vector<std::vector<float>>v( 2500, 3 );
	int i=0;
	for(i=0;i<2500;i++){
		for( int j=0;j<3;j++)
		{	myfile >> y;
		    v.at(i)(j) = y ;			
			cout << v.at(i)(j);
		}}



this code gives error:


1>orjin.cpp(18): error C2064: term does not evaluate to a function taking 1 arguments
1>orjin.cpp(19): error C2064: term does not evaluate to a function taking 1 arguments

the lines 18 and 19 are the lines:
1
2
v.at(i)(j) = y ;			
	cout << v.at(i)(j);
You need to use either:

v.at(i).at(j)

or

v[i][j]
thank you so much.
how do ı send some variable to v(i)(j)
how do ı send some variable to v(i)(j)


v[i][j] = someVariable;
should i not use v.push_back??

how to define v?
Topic archived. No new replies allowed.