简体   繁体   English

在C ++中使用符号位替换std :: vector的负值

[英]Replace negative values of std::vector using signbit in C++

I am trying to replace negative values of a std::vector with another known value using signbit, but it wont work. 我试图用signbit另一个已知值来代替一个std的负值:: vector的,但它不会工作。 The failing command is: 失败的命令是:

replace_if(myvector.begin(), myvector.end(), signbit, 999);

The error I get is: 我得到的错误是:

error: no matching function for call to ‘replace_if(std::vector<int>::iterator, std::vector<int>::iterator, <unresolved overloaded function type>, int)’

The std::vector is of type int, and from the error output I understand that it didn't find the function? std :: vector是int类型的,从错误输出中我知道它没有找到函数? I included "math.h" though.. Maybe signbit does not work with integers? 我包括了“ math.h”。.也许signbit不适用于整数?

If this wont work, can you propose an alternative in one line? 如果这样行不通,您可以在一行中提出替代方案吗?

Edit: Because the code is big I made an example which produces the same error. 编辑:因为代码很大,所以我举了一个例子,产生了同样的错误。

// replace_if example
#include <iostream>     // std::cout
#include <algorithm>    // std::replace_if
#include <vector>       // std::vector
#include <cmath>       //  std::signbit

int main () {
  std::vector<int> myvector;

  // set some values:
  for (int i=-10; i<10; i++) myvector.push_back(i);              

  std::replace_if (myvector.begin(), myvector.end(), std::signbit, 0); 

  std::cout << "myvector contains:";
  for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}

Thanks in advance! 提前致谢!

The error <unresolved overloaded function type> tells you that there are 错误<unresolved overloaded function type>告诉您
multiple overloads that could be used, but the compiler does not know which one. 可以使用多个重载,但是编译器不知道哪个重载。

One simple solution would be to use a lambda function instead of passing the function name with multiple overloads 一种简单的解决方案是使用lambda函数,而不是通过多个重载传递函数名

std::replace_if (myvector.begin(), myvector.end(), [](int i){return std::signbit(i);}, 999); 

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

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