vector<int> res;
时间: 2023-05-22 21:07:26 浏览: 57
vector<int> res 是一个 C++ 中的标准库容器,可以存储任意类型的元素,但在这种情况下,它被声明为一个 int 类型的 vector。这意味着它可以存储多个整数值,并且提供了一些有用的函数来访问和操作这些值。例如,您可以使用 push_back() 函数将新的整数值添加到 vector 的末尾,并使用 size() 函数获取 vector 中元素的数量。
相关问题
vector<int>& res
A vector is a container class in C++ that represents a dynamic array. It is defined in the standard library header <vector> and is part of the STL (Standard Template Library).
A vector<int> is a vector that stores integer values. It can be used to store a sequence of integers that can be accessed using an index. Here is an example of how to create and use a vector<int>:
```
#include <vector>
#include <iostream>
int main() {
std::vector<int> numbers = {1, 2, 3, 4, 5};
// Accessing elements
std::cout << numbers[0] << std::endl; // Output: 1
// Iterating over elements
for (auto num : numbers) {
std::cout << num << " ";
}
// Output: 1 2 3 4 5
// Adding elements
numbers.push_back(6);
// Removing elements
numbers.pop_back();
return 0;
}
```
In this example, we create a vector of integers called numbers and initialize it with some values. We then access elements using the [] operator and iterate over them using a for loop. We also demonstrate how to add and remove elements from the vector using the push_back() and pop_back() methods.
class Solution { public: vector<int> singleNumbers(vector<int> &nums) { vector<int> res; sort(nums.begin(), nums.end()); res.push_back(nums[0]); for (int i = 1; i < nums.size(); i++) { res.push_back(nums[i]); if (nums[i]== *(res.end() - 1)) { res.pop_back(); res.pop_back(); } } return res; } };哪里哟错
在判断重复的地方,应该使用 `res.back()` 而不是 `*(res.end() - 1)`。因为 `res.end() - 1` 是一个迭代器,它指向的是最后一个元素的下一个位置,使用 `*` 取出的值是未定义的,会导致错误。正确的实现应该是这样的:
```
class Solution {
public:
vector<int> singleNumbers(vector<int> &nums)
{
vector<int> res;
sort(nums.begin(), nums.end());
res.push_back(nums[0]);
for (int i = 1; i < nums.size(); i++)
{
res.push_back(nums[i]);
if (nums[i] == res.back())
{
res.pop_back();
res.pop_back();
}
}
return res;
}
};
```