‘]’标记之前的预期主表达式(二分搜索)
Expected primary-expression before ‘]’ token(bisection search)
我哪里做错了?当我编译这个时~
bs_01.cpp: In function ‘int main()’:
bs_01.cpp:29:25: error: expected primary-expression before ‘]’ token
cout << bisection_01(foo[]);
#include"std_lib_facilities.h"
using namespace std;
template <class T>
void bisection_01(T foo[])
{
int low, high,mid;
low = 0;
high = (foo.size()-1);
mid = (high+low)/2;
while(low!=high)
{
if (foo[mid]==0)
if (foo[mid+1]==0)
low = mid;
else
cout<<"The change has occured from "<<mid+1<<" to "<<mid+2;
if (foo[mid]==1)
if(foo[mid-1]==1)
high = mid;
else
cout<<"The change has occured from "<<mid<<" to "<<mid+1;
}
}
int main()
{
int foo[7] {0, 0, 0, 0, 1, 1, 1};
cout<<bisection_01(foo[]);
}
您不能将 foo[]
传递给函数。还有 bisection_01()
returns void
不能插入 ostream
;
此代码编译后似乎可以执行您想要的操作:
#include <iostream>
#include <array>
using namespace std;
template <class T, size_t size>
void bisection_01(std::array<T,size> foo)
{
int low, high,mid;
low = 0;
high = (size-1);
mid = (high+low)/2;
while(low!=high)
{
if (foo[mid]==0) {
if (foo[mid + 1] == 0)
low = mid;
else
std::cout << "The change has occured from " << mid + 1 << " to " << mid + 2;
}
if (foo[mid]==1) {
if (foo[mid - 1] == 1)
high = mid;
else
std::cout << "The change has occured from " << mid << " to " << mid + 1;
}
}
}
int main()
{
std::array<int,7> foo{0, 0, 0, 0, 1, 1, 1};
bisection_01(foo);
}
我哪里做错了?当我编译这个时~
bs_01.cpp: In function ‘int main()’: bs_01.cpp:29:25: error: expected primary-expression before ‘]’ token cout << bisection_01(foo[]);
#include"std_lib_facilities.h"
using namespace std;
template <class T>
void bisection_01(T foo[])
{
int low, high,mid;
low = 0;
high = (foo.size()-1);
mid = (high+low)/2;
while(low!=high)
{
if (foo[mid]==0)
if (foo[mid+1]==0)
low = mid;
else
cout<<"The change has occured from "<<mid+1<<" to "<<mid+2;
if (foo[mid]==1)
if(foo[mid-1]==1)
high = mid;
else
cout<<"The change has occured from "<<mid<<" to "<<mid+1;
}
}
int main()
{
int foo[7] {0, 0, 0, 0, 1, 1, 1};
cout<<bisection_01(foo[]);
}
您不能将 foo[]
传递给函数。还有 bisection_01()
returns void
不能插入 ostream
;
此代码编译后似乎可以执行您想要的操作:
#include <iostream>
#include <array>
using namespace std;
template <class T, size_t size>
void bisection_01(std::array<T,size> foo)
{
int low, high,mid;
low = 0;
high = (size-1);
mid = (high+low)/2;
while(low!=high)
{
if (foo[mid]==0) {
if (foo[mid + 1] == 0)
low = mid;
else
std::cout << "The change has occured from " << mid + 1 << " to " << mid + 2;
}
if (foo[mid]==1) {
if (foo[mid - 1] == 1)
high = mid;
else
std::cout << "The change has occured from " << mid << " to " << mid + 1;
}
}
}
int main()
{
std::array<int,7> foo{0, 0, 0, 0, 1, 1, 1};
bisection_01(foo);
}