-4

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.

baadal51
  • 19
  • 5
  • 4
    Begin by creating a [mre] to show us. Then buy [a couple of good beginners C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) because that so-called "competition" site isn't doing a good job at that (that's not the purpose of such sites). There are plenty of errors that tells us you need to study the very basics of C++ first. – Some programmer dude May 22 '23 at 10:11
  • 4
    What type does `.begin()` return? Surely not `unsigned int`. – HolyBlackCat May 22 '23 at 10:14

2 Answers2

2

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.

john
  • 85,011
  • 4
  • 57
  • 81
0

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 {};
    }
};
fabian
  • 80,457
  • 12
  • 86
  • 114