Slay
Slay

Reputation: 231

"No match for operator+"

I'm a beginner and getting back to programming after 2 months. Almost forgotten everything. The code I just tried to compile eliminates the second half of the vector usin iterators, if the character I want is found in the first half.

Here's the code:

#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
    vector<string> v;
    string s("hello world");
    char sought = 'e';
    v.push_back(s);
    auto mid = (v.begin() + v.end())/2;
    if (sought<*mid)
    {
        end = mid;
    }
    cout<<*v;

}

Please tell me what my mistake is.

Also, can programming be forgotten? Also, once proficient, do the concept stay forever, or does it require constant practice and reading?

Upvotes: 1

Views: 170

Answers (1)

NPE
NPE

Reputation: 500357

First of all, you cannot add iterators together (in v.begin() + v.end()). Instead, use

auto mid = v.begin() + v.size() / 2;

Secondly, end and it are undeclared.

Finally, you cannot erase part of a vector by assigning to "end". You need to use a different method (left as an exercise).

Upvotes: 6

Related Questions