Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无法转换<;int*,std::vector<;int>&燃气轮机’;至‘;int*';_C++_Function_Pointers_Vector_Compiler Errors - Fatal编程技术网

C++ 无法转换<;int*,std::vector<;int>&燃气轮机’;至‘;int*';

C++ 无法转换<;int*,std::vector<;int>&燃气轮机’;至‘;int*';,c++,function,pointers,vector,compiler-errors,C++,Function,Pointers,Vector,Compiler Errors,我试图通过使用pair.first和pair.second找到元素的上下限 我在使用gcc编译时遇到以下错误: zco.cpp: In function ‘int main()’: zco.cpp:136:47: error: cannot convert ‘__gnu_cxx::__normal_iterator<int*, std::vector<int> >’ to ‘int*’ in initialization int *a = lower_bound(x.

我试图通过使用pair.first和pair.second找到元素的上下限

我在使用gcc编译时遇到以下错误:

zco.cpp: In function ‘int main()’:
zco.cpp:136:47: error: cannot convert 
‘__gnu_cxx::__normal_iterator<int*, std::vector<int> >’ to ‘int*’ in 
initialization
int *a = lower_bound(x.begin(), x.end(),temp1);
                                           ^
zco.cpp:137:47: error: cannot convert 
‘__gnu_cxx::__normal_iterator<int*, std::vector<int> >’ to ‘int*’ in 
initialization
int *b = upper_bound(y.begin(), y.end(),temp2);
zco.cpp:在函数“int main()”中:
zco.cpp:136:47:错误:无法转换
“_gnu_cxx::_normal_iterator”到中的“int*”
初始化
int*a=下限(x.begin(),x.end(),temp1);
^
zco.cpp:137:47:错误:无法转换
“_gnu_cxx::_normal_iterator”到中的“int*”
初始化
int*b=上限(y.begin(),y.end(),temp2);
代码:

intmain(){
int N,X,Y;
scanf(“%d%d%d”,&N,&X,&Y);
向量n(n);

对于(int i=0;i您需要一个向量迭代器,而不是指向其数据类型的指针,如下所示:

std::vector<int>::iterator temp1;
std::vector<int>::iterator temp2;
std::vector::迭代器temp1;
std::vector::迭代器temp2;

有关更多信息,请查看的参考及其示例。

您需要一个向量迭代器,而不是指向其数据类型的指针,如下所示:

std::vector<int>::iterator temp1;
std::vector<int>::iterator temp2;
std::vector::迭代器temp1;
std::vector::迭代器temp2;

有关更多信息,请查看的引用及其示例。

以下是如何将迭代器转换为指针,首先取消对迭代器的引用,这将生成一个引用(在本例中为int),然后获取引用值的地址。这将提供:

std::vector<int>::iterator it_a = std::lower_bound(x.begin(), x.end(),temp1);
int& ref_a = *it_a;
int* ptr_a = &ref_a;

下面介绍如何将迭代器转换为指针,首先取消对迭代器的引用,这将生成一个引用(在您的例子中为int),然后获取引用值的地址。这将提供:

std::vector<int>::iterator it_a = std::lower_bound(x.begin(), x.end(),temp1);
int& ref_a = *it_a;
int* ptr_a = &ref_a;

a
b
类型错误,
lower_bound
返回迭代器,而不是指针,您可以使用自动类型推断来修复它,而不会有太多问题:
auto a(lower_bound(x.begin(),x.end(),temp1))大多数的大学教授仍然教C++,就像C++一样,教学生写代码,就像我们许多人花的时间打扫的一样。那些能做的,不能教的。这是一个来自某人的报价。无论如何,他们至少可以告诉他们的费用来检查RUTU。rn值由于一些模糊的原因,它们仍然会检查
new
的返回。
a
b
类型错误,
lower\u bound
返回一个迭代器,而不是指针,您可以使用自动类型推断来修复它,而不会有太多问题:
auto a(lower\u bound(x.begin(),x.end(),temp1))大多数的大学教授仍然教C++,就像C++一样,教学生写代码,就像我们许多人花的时间打扫的一样。那些能做的,不能教的。这是一个来自某人的报价。无论如何,他们至少可以告诉他们的费用来检查RUTU。rn值由于一些模糊的原因,他们仍然会检查
new
的返回。