如何解决C ++中向量错误的解决方案是什么?
/*
// Sample code to perform I/O:
#include <iostream>
using namespace std;
int main() {
int num;
cin >> num; // Reading input from STDIN
cout << "Input number is " << num << endl; // Writing output to STDOUT
}
// Warning: Printing unwanted or ill-formatted data to output will cause the test cases to fail
*/
// Write your code here
#include<iostream>
#include<string>
#include<vector>
#include <algorithm>
using namespace std;
typedef long long int ll;
vector<ll> v;
void make(ll num)
{
if(num>10000000000)
return ;
if(num!=0)
v.insert(num);
make(num*10+4);
make(num*10+7);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll num=0;
make(num);
ll n,ans=0;
cin>>n;
for(auto i:v)
{
for(auto j:v)
{
if(i<=n&&j<=n&&j>=i)
{
if(__gcd(i,j)==1)
ans++;
}
}
}
cout<<ans<<endl;
return 0;
}
错误:
In function 'void make(ll)':
30:21: error: no matching function for call to 'std::vector<long long int>::insert(ll&)'
69:0,20:
114:5: note: candidate: std::vector<_Tp,_Alloc>::iterator std::vector<_Tp,_Alloc>::insert(std::vector<_Tp,_Alloc>::const_iterator,const value_type&) [with _Tp = long long int; _Alloc = std::allocator<long long int>; std::vector<_Tp,_Alloc>::iterator = __gnu_cxx::__normal_iterator<long long int*,std::vector<long long int> >; typename std::_Vector_base<_Tp,_Alloc>::pointer = long long int*; std::vector<_Tp,_Alloc>::const_iterator = __gnu_cxx::__normal_iterator<const long long int*,std::vector<long long int> >; typename __gnu_cxx::__alloc_traits<typename std::_Vector_base<_Tp,_Alloc>::_Tp_alloc_type>::const_pointer = const long long int*; std::vector<_Tp,_Alloc>::value_type = long long int]
:
114:5: note: candidate expects 2 arguments,1 provided
64:0,20:
1042:7: note: candidate: std::vector<_Tp,std::vector<_Tp,_Alloc>::value_type&&) [with _Tp = long long int; _Alloc = std::allocator<long long int>; std::vector<_Tp,_Alloc>::value_type = long long int]
1042:7: note: candidate expects 2 arguments,1 provided
1059:7: note: candidate: std::vector<_Tp,std::initializer_list<_Tp>) [with _Tp = long long int; _Alloc = std::allocator<long long int>; std::vector<_Tp,_Alloc>::_Tp_alloc_type>::const_pointer = const long long int*]
1059:7: note: candidate expects 2 arguments,1 provided
1084:7: note: candidate: std::vector<_Tp,_Alloc>::size_type,_Alloc>::size_type = long unsigned int; std::vector<_Tp,_Alloc>::value_type = long long int]
1084:7: note: candidate expects 3 arguments,1 provided
1128:2: note: candidate: template<class _InputIterator,class> std::vector<_Tp,_InputIterator,_InputIterator) [with _InputIterator = _InputIterator; <template-parameter-2-2> = <template-parameter-1-2>; _Tp = long long int; _Alloc = std::allocator<long long int>]
1128:2: note: template argument deduction/substitution failed:
30:21: note: candidate expects 3 arguments,1 provided<br>
如何解决问题
解决方法
在make()函数中,尝试使用v.push_back(num)
代替v.insert(num)
,它应该起作用。
您在vector::insert
中缺少第一个参数,即应将新元素放置在其后的位置。
如果您不想按照上一个答案的建议使用vector::push_back
,请使用以下MWE:
#include<iostream>
#include<string>
#include<vector>
#include <algorithm>
using namespace std;
typedef long long int ll;
vector<ll> v;
void make(ll num)
{
if(num>10000000000)
return;
if(num!=0)
v.insert(v.begin(),num); // iterator to insert position added
make(num*10+4);
make(num*10+7);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll num=0;
make(num);
ll n,ans=0;
cout << "Input number: " << endl;
cin>>n;
for(auto i:v)
{
for(auto j:v)
{
if(i<=n&&j<=n&&j>=i)
{
if(__gcd(i,j)==1)
ans++;
}
}
}
cout<< "answer = " << ans <<endl;
return 0;
}
这是做什么用的?随机数生成器?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。