C/C++ Need help coding with assigning the size of a Vector (C++)

  • Thread starter Thread starter EvanET
  • Start date Start date
  • Tags Tags
    Coding Vector
AI Thread Summary
To assign the size of the vector `sensorReadings` to `currentSize`, the correct syntax is using the assignment operator `=` rather than the equality operator `==`. The method `sensorReadings.size()` retrieves the current size of the vector. The correct line of code should be `currentSize = sensorReadings.size();`. The confusion arose from misinterpreting the assignment process, leading to an attempt to assign a value to the size method instead. Understanding the proper use of assignment versus comparison is crucial in resolving the error.
EvanET
Messages
10
Reaction score
0
I am prompted to: Assign the size of vector sensorReadings to currentSize.

Assigning is the double equal sign (==) but i get an extremely long error

i am given this much code:

#include <iostream>
#include <vector>
using namespace std;

int main() {
vector<int> sensorReadings(4);
int currentSize = 0;

sensorReadings.resize(10);
//begin student answer
sensorReadings.resize(10) == currentSize; //my answer
//end student answer
cout << "Number of elements: " << currentSize << endl;

return 0;
}
 
Technology news on Phys.org
EvanET said:
I am prompted to: Assign the size of vector sensorReadings to currentSize.

Assigning is the double equal sign (==) but i get an extremely long error

i am given this much code:

#include <iostream>
#include <vector>
using namespace std;

int main() {
vector<int> sensorReadings(4);
int currentSize = 0;

sensorReadings.resize(10);
//begin student answer
sensorReadings.resize(10) == currentSize; //my answer
//end student answer
cout << "Number of elements: " << currentSize << endl;

return 0;
}

Hi EvanET! :)

Assigning means using an assignment (=) instead of a comparison for equality (==).
So we should have something like [M]currentSize = ?[/M].
Does [M]vector<int>[/M] have a method to retrieve its size, so that we can assign it to [M]currentSize[/M]?
 
I like Serena said:
Hi EvanET! :)

Assigning means using an assignment (=) instead of a comparison for equality (==).
So we should have something like [M]currentSize = ?[/M].
Does [M]vector<int>[/M] have a method to retrieve its size, so that we can assign it to [M]currentSize[/M]?

are you meaning like -> .resize?
 
EvanET said:
are you meaning like -> .resize?

I'm thinking more like [M].size()[/M]. (Thinking)
 
I like Serena said:
I'm thinking more like [M].size()[/M]. (Thinking)

Hmmm.. ok.

Code:
sensorReadings.size() = currentSize;

so like that?
 
EvanET said:
Hmmm.. ok.

Code:
sensorReadings.size() = currentSize;

so like that?

Well, we can really assign to the size() method...
It should be more like:
Code:
currentSize = sensorReadings.size();
 
I like Serena said:
Well, we can really assign to the size() method...
It should be more like:
Code:
currentSize = sensorReadings.size();
WOW(Headbang)

i just had it backwards this whole time (Giggle)

i previously tried it as:
Code:
sensorReadings.size() = currentSize;

i guess i just need to read it right to left, so to speak.
 

Similar threads

Replies
1
Views
3K
Replies
22
Views
3K
Replies
5
Views
3K
Replies
8
Views
2K
Replies
1
Views
1K
Replies
13
Views
2K
Replies
23
Views
2K
Back
Top