C++ C++;矢量参考转换

C++ C++;矢量参考转换,c++,pointers,vector,type-conversion,pointer-arithmetic,C++,Pointers,Vector,Type Conversion,Pointer Arithmetic,以下代码拒绝编译 编译器抱怨从向量引用到int指针的转换,但我不明白为什么。这个问题有什么解决办法吗 bool inSortedVectorHelper(int* front, int* back, int num) { if(front==back || front > back ) return false; int* mid = front+(back-front)/2; if((*mid) == num) return true; else if

以下代码拒绝编译

编译器抱怨从向量引用到int指针的转换,但我不明白为什么。这个问题有什么解决办法吗

bool inSortedVectorHelper(int* front, int* back, int num)
{
  if(front==back || front > back )
    return false;

  int* mid = front+(back-front)/2;
  if((*mid) == num)
    return true;

  else if(num < (*mid))
    return inSortedVectorHelper(front,mid-1,num);
else
    return inSortedVectorHelper(mid+1,back,num);
}


bool inSortedVector(const std::vector<int> &sorted, int num)
{
  int* front,back;
  front = sorted.front();//both return references
  back = sorted.back();

  return inSortedVectorHelper(front, back, num);
}
bool inSortedVectorHelper(int*front,int*back,int num)
{
如果(前==后| |前>后)
返回false;
int*mid=front+(后-前)/2;
如果((*mid)==num)
返回true;
否则如果(数值<(*mid))
返回InserteDVectorHelper(前、中1、num);
其他的
返回inSortedVectorHelper(mid+1,back,num);
}
bool insertedvector(const std::vector&sorted,int num)
{
int*正面、背面;
front=sorted.front();//两个返回引用
back=sorted.back();
返回inSortedVectorHelper(前、后、num);
}
精确编译器输出:

P5.cpp: In function ‘bool inSortedVector(const std::vector<int>&, int)’:
P5.cpp:62:9: error: invalid conversion from ‘__gnu_cxx::__alloc_traits<std::allocator<int> >::value_type {aka int}’ to ‘int*’ [-fpermissive]
front = sorted.front();//both return references
      ^
P5.cpp:65:47: error: invalid conversion from ‘int’ to ‘int*’ [-fpermissive]
return inSortedVectorHelper(front, back, num);
                                           ^
P5.cpp: In function ‘bool inSortedVector(const std::vector<int>&, int)’:
P5.cpp:62:9: error: invalid conversion from ‘__gnu_cxx::__alloc_traits<std::allocator<int> >::value_type {aka int}’ to ‘int*’ [-fpermissive]
front = sorted.front();//both return references
      ^
P5.cpp:65:47: error: invalid conversion from ‘int’ to ‘int*’ [-fpermissive]
return inSortedVectorHelper(front, back, num);
P5.cpp:bool insertedvector(const std::vector&,int)函数中:
P5.cpp:62:9:错误:从“\uu gnu\u cxx::\uu alloc\u traits::value\u type{aka int}”到“int*”[-fppermissive]的转换无效
front=sorted.front()//两者都返回引用
^
P5.cpp:65:47:错误:从“int”到“int*”的转换无效[-fpermissive]
返回inSortedVectorHelper(前、后、num);
^
P5.cpp:在函数“bool insertedvector(const std::vector&,int)”中:
P5.cpp:62:9:错误:从“\uu gnu\u cxx::\uu alloc\u traits::value\u type{aka int}”到“int*”[-fppermissive]的转换无效
front=sorted.front()//两者都返回引用
^
P5.cpp:65:47:错误:从“int”到“int*”的转换无效[-fpermissive]
返回inSortedVectorHelper(前、后、num);
我不敢相信我在使用一个简单的二进制搜索函数时会遇到这么多麻烦,所以非常感谢您的帮助。谢谢

您想要:

int *front, *back;
front = &sorted.front();
back = &(sorted.back()) + 1;

front
back
返回对int的引用(作为您的注释文档)。您的变量是指针。你应该写:

int *front, *back;
front = &(sorted.front()); //both return references
back = &(sorted.back());

此外,对于非布尔向量,
&(sorted.front())
相当于
sorted.data()

这应该可以在没有使用指针的丑陋代码的情况下工作:

bool inSortedVector(const std::vector<int> &sorted, int num)
{
    return std::binary_search( sorted.begin(), sorted.end(), num );
}
bool insertedvector(const std::vector&sorted,int num)
{
返回std::二进制搜索(sorted.begin(),sorted.end(),num);
}

如果您想重新实现
std::binary_search
,您应该了解如何使用迭代器在容器上正确实现它。

int*front,back
front是指针类型,back不是,对于您的情况,两者都不必是指针类型。或者您可能打算将
begin()
end
与迭代器一起使用?//这两个函数都返回引用“好的,那么为什么要将结果分配给指针呢?另外,我打赌
int*front,back
没有做你认为它正在做的事情。除了这会使
back
指向最后一个项目,而不是一个过去的项目。只有
front
int*front,back中的指针
back
只是一个int。这一点很明显,因为
back
没有错误,看起来不错,但是一个简短的解释可以帮助其他人理解基本逻辑,从而避免将来出现此类错误。:)不幸的是,我无法使用算法library@Tyler至少您可以将其用作实现算法的适当模型。