How to fix " no viable conversion from 'std::vector<int, std::allocator<int>>::iterator' to 'unsigned int' "?

huangapple go评论71阅读模式
英文:

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 &#39;std::vector&lt;int, std::allocator&lt;int&gt;&gt;::iterator&#39; (aka &#39;__normal_iterator&lt;int *, std::vector&lt;int, std::allocator&lt;int&gt;&gt;&gt;&#39;) to &#39;unsigned int&#39;
    for(unsigned int i=nums.begin();i&lt;nums.end();i++)

Here is my code:

class Solution {
public:
    vector&lt;int&gt; twoSum(vector&lt;int&gt;&amp; nums, int target) {
        for(unsigned int i=nums.begin();i&lt;nums.end();i++)
        {
            for(int j=i+1;j&lt;=nums.end();j++)
            {
                if(nums[i]+nums[j]==target)
                {
                    vector&lt;int&gt; 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&lt;nums.size();i++)
    {
        for(int j=i+1;j&lt;nums.size();j++)
        {

You are getting confused between iterators and indexes. You also made a mistake using j&lt;= instead of j&lt;.

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&lt;nums.size();i++)
    {
        for(size_t j=i+1;j&lt;nums.size();j++)
        {

Another serious mistake is here

                vector&lt;int&gt; 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&lt;int&gt; 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&lt;int&gt; 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&lt;int&gt; twoSum(vector&lt;int&gt; const&amp; nums, int const target)
    {
        if (nums.size() &gt; 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&lt;int&gt;(std::distance(nums.begin(), outerIter)),
                            static_cast&lt;int&gt;(std::distance(nums.begin(), innerIter))
                        };
                    }
                }
            }
        }
        return {};
    }
};

huangapple
  • 本文由 发表于 2023年5月22日 18:08:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76305089.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定