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
Click For Summary

Discussion Overview

The discussion revolves around a coding issue related to assigning the size of a vector in C++. Participants are exploring the correct syntax and method to retrieve and assign the size of the vector sensorReadings to the variable currentSize.

Discussion Character

  • Homework-related
  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • One participant initially attempts to use the equality operator (==) for assignment, which leads to an error.
  • Another participant clarifies that assignment should use a single equal sign (=) instead of the double equal sign (==).
  • There is a suggestion to use a method from the vector class to retrieve its size, prompting further exploration of the correct method.
  • Participants discuss the appropriate method to use, with one suggesting .resize() and another proposing .size().
  • A later reply emphasizes that the correct assignment should be currentSize = sensorReadings.size();, correcting the misunderstanding about assigning to the size method.
  • One participant expresses realization about having the assignment backwards and acknowledges the need to read the expression correctly.

Areas of Agreement / Disagreement

Participants generally agree on the correct method to retrieve the size of the vector, but there was initial confusion regarding the syntax for assignment. The discussion reflects a progression from misunderstanding to clarity without any unresolved disagreements.

Contextual Notes

There is a focus on the syntax of assignment in C++ and the methods available for the vector class, which may depend on the participants' familiarity with the language.

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;
}
 
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
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?
 
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 ·
Replies
1
Views
3K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 10 ·
Replies
10
Views
3K
Replies
12
Views
3K
  • · Replies 36 ·
2
Replies
36
Views
6K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 40 ·
2
Replies
40
Views
3K