问题描述
我添加了 bits/stdc+.h 和 vector。 这个错误还是来了。 谁能告诉我为什么会这样。
#include <bits/stdc++.h>
#include<vector>
void rotate(int arr[],int n);
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
int a[n],i;
for(i=0;i<n;i++)
scanf("%d",&a[i]);
rotate(a,n);
for (i = 0; i < n; i++)
printf("%d ",a[i]);
printf("\n");
}
return 0;
}
// } Driver Code Ends
//User function Template for C++
void rotate(int arr[],int n)
{
vector<int> a;
a[0] = arr[n-1];
for(int i = 0 ; i<n-1 ;i++)
{
a.insert(a.back(),arr[i]);
}
for(int j : a)
cout<<j;
}
main.cpp:30:5: error: ‘vector’ was not declared in this scope
vector<int> a;
^~~~~~
解决方法
遵循这些:(已编辑)
- (您的问题的解决方案)使用,使用命名空间 std,因为这意味着如果编译器发现未在当前作用域中声明的内容,那么它会去检查 std。
- 不要混合使用 c 和 c++ 语法。使用 printf 或 cout。
另请检查此答案的第 1 条评论,因为您应该了解有关“使用命名空间 std”和“cout/cin”的一些信息。
- 无需工作两次,您也可以一次声明和定义您的函数。
解决方案(但其他部分有错误)
#include <bits/stdc++.h>
using namespace std;
void rotate(int arr[],int n)
{
vector<int> a;
a[0] = arr[n - 1];
for (int i = 0 ; i < n - 1 ; i++)
{
a.insert(a.back(),arr[i]); // ITS YOUR SYNTAX,CONSIDER TO UPDATE IT
}
for (auto &it : a)
cout << it;
}
int main()
{
int t;
cin>>t;
while (t--)
{
int n;
cin>>t;
int a[n],i;
for (i = 0; i < n; i++)
cin>>a[i];
rotate(a,n);
for (i = 0; i < n; i++)
cout<<a[i];
cout<<"\n";
}
return 0;
}
检查第 9 行,
看看它是否正确。 a.insert(a.back(),arr[i]); 错误 你在那里做错了什么。检查这个声明
错误:'vector' 未在此范围内声明
使用命名空间解决
如果你喜欢这个答案。请考虑勾选这个答案。:)