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

  • Context: C/C++ 
  • Thread starter Thread starter EvanET
  • Start date Start date
  • Tags Tags
    Coding Vector
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
6 replies · 7K views
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
count << "Number of elements: " << currentSize << endl;

return 0;
}
 
Physics 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
count << "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?
 
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.