我正在尝试解决此问题in Codility ...这是代码:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> solution(vector<int> &A, int k);
vector<int> A;
A.push_back(3);
A.push_back(5);
A.push_back(7);
A.push_back(9);
A.push_back(2);
int k;
rotate(A.rbegin(),A.rbegin()+k, A.rend());
虽然我的编译器可以毫无问题地进行编译和运行,但是编译提示我“错误:'A'未命名类型”。这是我的编译器用来检查它的代码:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> myVector;
myVector.push_back(3);
myVector.push_back(5);
myVector.push_back(7);
myVector.push_back(9);
myVector.push_back(2);
for(unsigned i=0;i<myVector.size();i++)
{
cout<<myVector[i]<<" ";
}
cout<<endl;
int k;
cout<<"Insert the times of right rotation:";
cin>>k;
rotate(myVector.rbegin(),myVector.rbegin()+k, myVector.rend());
for(unsigned i=0;i<myVector.size();i++)
{
cout<<myVector[i]<<" ";
}
}
编译器输出:
func.cpp:9:3: error: 'A' does not name a type
A.push_back(3);
^
func.cpp:10:3: error: 'A' does not name a type
A.push_back(5);
^
func.cpp:11:3: error: 'A' does not name a type
A.push_back(7);
^
func.cpp:12:3: error: 'A' does not name a type
A.push_back(9);
^
func.cpp:13:3: error: 'A' does not name a type
A.push_back(2);
^
func.cpp:16:9: error: expected constructor, destructor, or type conversion before '(' token
rotate(A.rbegin(),A.rbegin()+k, A.rend());
^
func.cpp:18:1: error: expected declaration before '}' token
}
^
Detected some errors.
我认为您有很多问题,并做了一些假设这是工作代码
如果您不想使用<algorithm>
中的旋转功能。
您只能在函数外部定义或声明符号。您的A.push_back(3);
等不是定义或声明。这就是为什么您会得到有趣的错误消息的原因:定义和声明以类型开头,并且由于这些行不在函数之外,因此编译器希望看到一个类型,而A
则不是。
目标C解O(n * k)-一对一方法
目标C解O(n)-基于逆的解