英文:
How to fix " no viable conversion from 'std::vector<int, std::allocator<int>>::iterator' to 'unsigned int' "?
问题
Line 4: Char 26: error: 无法从 'std::vector<int, std::allocator<int>>::iterator' (也就是 '__normal_iterator<int *, std::vector<int, std::allocator<int>>>' 的别名) 转换为 'unsigned int'
for(unsigned int i=nums.begin();i<nums.end();i++)
英文:
I am new to Vector while solving Twosum problem from leetcode, encountered following Compilation error :
Line 4: Char 26: error: no viable conversion from 'std::vector<int, std::allocator<int>>::iterator' (aka '__normal_iterator<int *, std::vector<int, std::allocator<int>>>') to 'unsigned int'
for(unsigned int i=nums.begin();i<nums.end();i++)
Here is my code:
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
for(unsigned int i=nums.begin();i<nums.end();i++)
{
for(int j=i+1;j<=nums.end();j++)
{
if(nums[i]+nums[j]==target)
{
vector<int> vec;
vec[0]=i;
vec[1]=j;
break;
}
}
}
return vec;
}
};
I tried googling this but didn't get much. Just wanted to know what does this error mean and how to fix it.
答案1
得分: 2
你明显想写的是
for(size_t i=0;i<nums.size();i++)
{
for(size_t j=i+1;j<nums.size();j++)
{
你在迭代器和索引之间混淆了。此外,你使用了j<=
而不是j<
,这是一个错误。
此外,你应该注意到向量索引的类型是size_t
,所以上面的代码最好这样写
for(size_t i=0;i<nums.size();i++)
{
for(size_t j=i+1;j<nums.size();j++)
{
另一个严重的错误在于
vector<int> vec;
vec[0]=i;
vec[1]=j;
这是一个常见但毫无根据的观念,即向量在分配给不存在的索引时会自动调整大小。如果你想要一个大小为2的向量,那么应该这样创建
vector<int> vec(2);
vec[0]=i;
vec[1]=j;
或者以大小零(默认值)创建它,然后使用push_back
添加新的项目。
vector<int> vec;
vec.push_back(i);
vec.push_back(j);
最后一个错误(与向量无关)是你在循环内部声明了向量,但你试图在循环外部返回它,这是不允许的。
英文:
Clearly what you meant to write is
for(unsigned int i=0;i<nums.size();i++)
{
for(int j=i+1;j<nums.size();j++)
{
You are getting confused between iterators and indexes. You also made a mistake using j<=
instead of j<
.
Also you should note that the type of a vector index is size_t
, so the above would be better written as
for(size_t i=0;i<nums.size();i++)
{
for(size_t j=i+1;j<nums.size();j++)
{
Another serious mistake is here
vector<int> vec;
vec[0]=i;
vec[1]=j;
This is the common but completely unjustified belief that vectors resize themselves when you assign to an index that does not exist. If you want a vector of size 2 then create it so
vector<int> vec(2);
vec[0]=i;
vec[1]=j;
Or create it at size zero (the default) and add the new items using push_back
.
vector<int> vec;
vec.push_back(i);
vec.push_back(j);
A final mistake (nothing to do with vectors specifically) is that you've declared your vector inside the loop, but you are trying to return it outside the loop. That's not allowed.
答案2
得分: 0
编译器基本上会告诉你出了什么问题:你试图使用一个向量作为索引,而不是只使用迭代器或者只使用索引来访问向量的元素。
以下是使用迭代器进行元素访问的版本:
class Solution
{
public:
vector<int> twoSum(vector<int> const& nums, int const target)
{
if (nums.size() > 1)
{
for (auto outerIter = nums.begin(), outerEnd = nums.end() - 1; outerIter != outerEnd; ++outerIter)
{
for (auto innerIter = outerIter + 1; innerIter != nums.end(); ++innerIter)
{
if (*outerIter + *innerIter == target)
{
return {
static_cast<int>(std::distance(nums.begin(), outerIter)),
static_cast<int>(std::distance(nums.begin(), innerIter))
};
}
}
}
}
return {};
}
};
英文:
The compiler pretty much tells you what's wrong: You're trying to use a vector as an index instead of working with iterators only or with indices only when it comes to accessing the elements of the vector.
Here's a version using iterators for element access:
class Solution
{
public:
vector<int> twoSum(vector<int> const& nums, int const target)
{
if (nums.size() > 1)
{
for (auto outerIter = nums.begin(), outerEnd = nums.end() - 1; outerIter != outerEnd; ++outerIter)
{
for (auto innerIter = outerIter + 1; innerIter != nums.end(); ++innerIter)
{
if (*outerIter + *innerIter == target)
{
return {
static_cast<int>(std::distance(nums.begin(), outerIter)),
static_cast<int>(std::distance(nums.begin(), innerIter))
};
}
}
}
}
return {};
}
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论