C++ What type of pointer should I use to store output address of `algorithm` `remove` function?

Viewed 57

I'm trying to store the address returned by the algorithm remove function in C++ in a variable, but have failed to find one. I've tried int* and char*. Both threw errors.

Using Visual Studio CL, the error is: error C2440: '=': cannot convert from '_FwdIt' to 'int *'

Using MinGW, the error is: cannot convert '__gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >' to 'int*' in assignment

How should I store such an address?

The code I'm trying:

#include <stdio.h>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main (void) {
  string line ("This is an example sentence.");
  int* newEOL;
  newEOL = remove(line.begin(), line.end(), ' ');
  printf("%p\n", newEOL);
}
1 Answers

Why do you think it's a pointer?

As evidenced here: https://en.cppreference.com/w/cpp/algorithm/remove it's an iterator, which can be implemented as a pointer, but doesn't have to.

You can use the auto keyword if you don't want to specify the type explicitly.

Related