Being a beginner in C++ I am facing a difficulty to understand this thing:
vector<int> Solution::primesum(int n)
Code:
vector<int> Solution::primesum(int n){
vector<bool> isprime(n+1);
for(int i=0;i<=n;i++)
isprime[i]=1;
isprime[0]=0;
isprime[1]=0;
for(int i=2;i*i<=n;i++){
if(isprime[i]){
for(int j=i*2;j<=n;j+=i)
isprime[j]=0;
}
}
vector<int>ans(2);
for(int i=2;i<=n/2;i++){
if(isprime[i] && isprime[n-i]){
ans[0]=i;
ans[1]=n-i;
return ans;
}
}
}
Somewhere in your code there must be a class Solution
which has a member function primesum
taking an integer argument and returning a vector of integer type:
class Solution
{
public:
vector<int> primesum(int);
};
Only then you can define it outside using scope resolution operator ::
:
vector<int> Solution::primesum(int n)
{ // Definition }
Well, the solution means a class named "Solution" that represents a custom data type. And the primesum(int n) is a member function in the definition of the class with the return type, While the vector is a data type used as an enhanced version of int[] or char[](or string).
class Solution
{
public:
vector<int> primesum(int) //just like int* primesum(int); amount to returning a array
...
};
Solution::vector<int> Solution::primesum(int n){
...
}
If possoble, give more details for us to understand and answer your question.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.