C++ 引用绑定到类型为“value_type”的空指针
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47947956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
reference binding to null pointer of type 'value_type'
提问by lingbozf
This is leetcode 26. Given a sorted array, remove the duplicates in-place such that each element appear only once and return the new length. An example is given nums = [1,1,2]
, the function should return [1,2]
.
这是 leetcode 26。给定一个已排序的数组,就地删除重复项,使每个元素只出现一次并返回新的长度。给出nums = [1,1,2]
了一个例子,函数应该返回[1,2]
。
Below is my code. I delete all the other duplicates, just leave one of them. However I always got an error of reference binding to null pointer of type 'value_type'
when submitting. I would appreciate if anyone can help me with this!
下面是我的代码。我删除了所有其他重复项,只留下其中一个。但是我总是reference binding to null pointer of type 'value_type'
在提交时出错。如果有人可以帮助我,我将不胜感激!
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int i = 0;
while(i < nums.size() - 1) {
if (nums[i] == nums[i + 1]) {
nums.erase(nums.begin() + i);
}
else i++;
}
return nums.size();
}
};
回答by 0x499602D2
vector<T>::size()
returns a value of type size_t
, which is an unsigned type. Let's say the vector passed in is empty and therefore the vector's length is 0. nums.size() - 1
will cause integer underflow and you will actually be comparing 0
with a very large positive number. This will evaluate to true causing the loop to run and i
going pass the array bounds.
vector<T>::size()
返回一个 type 值size_t
,它是一个无符号类型。假设传入的向量为空,因此向量的长度为 0。nums.size() - 1
将导致整数下溢,您实际上将0
与非常大的正数进行比较。这将评估为 true 导致循环运行并i
通过数组边界。
To fix this you can cast nums.size()
to int
preemptively or store the size in an integer variable and compare with that.
要解决此问题,您可以强制转换nums.size()
为int
抢占式或将大小存储在整数变量中并与之进行比较。
回答by R Sahu
The function, as posted, works fine for a vector whose elements are [1 1 2]
. See https://ideone.com/ppuRg5.
正如发布的那样,该函数适用于元素为 的向量[1 1 2]
。请参阅https://ideone.com/ppuRg5。
However, one problem I see in your function is that if you pass it an empty vector, it's going to run into problems.
但是,我在您的函数中看到的一个问题是,如果您向它传递一个空向量,它将遇到问题。
while(i < nums.size() - 1)
will be a problem when nums
is empty. You can preemptively avoid that problem by returning from the function immediately if you find that it is an empty vector.
nums
为空时会出现问题。如果您发现它是一个空向量,您可以通过立即从函数返回来抢先避免该问题。
Also, use an unsigned type for i
to avoid compiler warnings about comparing signed and unsigned types.
此外,使用无符号类型i
来避免编译器关于比较有符号和无符号类型的警告。
int removeDuplicates(std::vector<int>& nums) {
if ( nums.empty() )
{
return 0;
}
unsigned int i = 0;
while(i < nums.size() - 1) {
if (nums[i] == nums[i + 1]) {
nums.erase(nums.begin() + i);
}
else i++;
}
return nums.size();
}
回答by Rain Forest
This is not an answer to your question, but it would be more efficient solution to the problem if you didn't have to resize your vector each time you find a duplicate. Just to give you an idea, you could have two iterators i and j, i keeping the index of the last unique element of your solution vector and j iterating through the vector. When j points to a value not in the first i elements, copy that to v[i]. And once you are done, delete everything from the j-th place onwards.
这不是您问题的答案,但如果您不必在每次找到重复项时都调整向量的大小,这将是更有效的问题解决方案。只是为了给您一个想法,您可以有两个迭代器 i 和 j,我保留解决方案向量的最后一个唯一元素的索引,并且 j 遍历向量。当 j 指向不在前 i 个元素中的值时,将其复制到 v[i]。完成后,从第 j 个位置开始删除所有内容。
回答by alexmosk25
In my case the reason of this error message was segmentation fault.
在我的情况下,此错误消息的原因是分段错误。
ExampleFor empty input:
示例对于空输入:
string longestCommonPrefix(vector<string>& strs) {
auto start = strs[0];
It works OK, when I add check for empty input
它工作正常,当我添加检查空输入时
string longestCommonPrefix(vector<string>& strs) {
if (strs.size() == 0) {
return "";
}
auto start = strs[0];
回答by ansar
class Solution {
public:
int removeDuplicates(vector < int > & nums) {
for (int i = 1; i < nums.size(); i++) {
if (nums[i] == nums[i - 1]) {
nums.erase(nums.begin() + i);
i--;
}
}
return nums.size();
}
};