简体   繁体   English

C++ 我应该使用什么类型的指针来存储 output `algorithm` `remove` function 的地址?

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

I'm trying to store the address returned by the algorithm remove function in C++ in a variable, but have failed to find one.我正在尝试将algorithm remove function 中的 C++ 中的地址存储在一个变量中,但找不到一个。 I've tried int* and char* .我试过int*char* Both threw errors.两者都抛出错误。

Using Visual Studio CL, the error is: error C2440: '=': cannot convert from '_FwdIt' to 'int *'使用 Visual Studio CL,错误是: 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使用 MinGW,错误是: 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);
}

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.正如这里所证明的: https://en.cppreference.com/w/cpp/algorithm/remove它是一个迭代器,可以实现为指针,但不是必须的。

You can use the auto keyword if you don't want to specify the type explicitly.如果您不想明确指定类型,可以使用auto关键字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM