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
SUMMARY

The discussion centers on correctly assigning the size of a C++ vector, specifically the vector named sensorReadings, to the variable currentSize. The user initially attempted to use the equality operator (==) instead of the assignment operator (=), leading to errors. The correct syntax is currentSize = sensorReadings.size();, which retrieves the current size of the vector and assigns it to currentSize.

PREREQUISITES
  • Understanding of C++ syntax and operators
  • Familiarity with the C++ Standard Library, specifically std::vector
  • Knowledge of methods associated with std::vector, particularly size()
  • Basic programming concepts such as variable assignment
NEXT STEPS
  • Learn about C++ vector methods, focusing on resize() and size()
  • Explore C++ operator overloading and the differences between assignment and comparison operators
  • Investigate error handling in C++ to better understand common compilation issues
  • Practice coding with C++ vectors through exercises that involve dynamic resizing and element access
USEFUL FOR

C++ beginners, educators teaching programming concepts, and developers looking to enhance their understanding of vector operations in C++.

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 36 ·
2
Replies
36
Views
6K
Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K