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

In summary: WOW(Headbang)In summary, Evan tries to assign the size of the vector sensorReadings to currentSize but gets an error.
  • #1
EvanET
10
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
  • #2
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]?
 
  • #3
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?
 
  • #4
EvanET said:
are you meaning like -> .resize?

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

Hmmm.. ok.

Code:
sensorReadings.size() = currentSize;

so like that?
 
  • #6
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();
 
  • #7
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.
 

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

1. How do I assign the size of a Vector in C++?

To assign the size of a Vector in C++, you can use the resize() function. This function takes in an integer as a parameter and resizes the Vector to that specified size. For example, if you want to resize a Vector named myVector to a size of 10, you would use myVector.resize(10).

2. What happens if I assign a size that is larger than the current size of the Vector?

If you assign a size that is larger than the current size of the Vector, the additional elements will be initialized to their default values. For primitive data types such as int or double, the default value is 0. For objects, the default value is the default constructor of that object.

3. Can I change the size of a Vector after it has been initialized?

Yes, you can change the size of a Vector after it has been initialized. You can use the resize() function to change the size at any point in your code. Keep in mind that resizing a Vector can be a costly operation as it involves copying elements to a new location in memory.

4. How do I check the current size of a Vector?

You can use the size() function to check the current size of a Vector. This function returns an integer representing the number of elements in the Vector. For example, if you have a Vector named myVector, you can use myVector.size() to get the current size.

5. Can I assign a negative size to a Vector?

No, you cannot assign a negative size to a Vector in C++. The resize() function will throw an error if you try to pass a negative value as the size parameter. Vectors in C++ are dynamically sized containers and cannot have a negative size.

Similar threads

  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
3
Views
782
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
13
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
10
Views
1K
Back
Top