Chrono Time Counter Issue in C++ Error: No match for operator
Chrono Time Counter Issue in C++ Error: No match for operator
#include <iostream>
#include <chrono>
#include <unistd.h>
using namespace std;
void SieveOfEratosthenes (int n)
{
bool prime[n+1], flag=true;
int counter=0, ct=0;
for (int i =0;i<n+1;i++){
prime[i]=true;
}
for (int p=2; p*p<=n; p++)
{
if (prime[p]==true)
{
for (int i=p*2; i<=n; i += p)
{
prime[i] = false;
}
}
}
// Print all prime numbers
for (int p=2; p<=n; p++)
{
if (flag)
{
auto begin=chrono::high_resolution_clock::now();
}
if (prime[p])
{
cout << p << " ";
counter+=1;
flag=false;
}
if(counter==10)
{
auto end=chrono::high_resolution_clock::now();
auto duration=chrono::nanoseconds(end-begin);
cout<<"Time elapsed:"<<duration.count();
counter=0;
flag=true;
}
}
cout<<endl;
}
int main()
{
int n;
cout<<"Type a number:";
cin>>n;
cout<<endl<<"Following are the prime numbers smaller or equal to:"<<n<<endl;
SieveOfEratosthenes(n);
return 0;
}
// Driver Program to test above function
这是一种查找小于 n 的素数(从键盘输入)的算法,我想得到每 10 个素数的时间 found.I 出现运算符错误并且它在(结束-开始)。我无法理解错误在哪里 is.And 我已经尝试将函数写在一个单独的 .o 文件中,但仍然得到 nothing.Any 帮助,将不胜感激。!
enter image description here
begin
定义在 if
块内,无法被 end-begin
访问。一个简单的解决方法是将定义移出 for
-loop:
decltype(chrono::high_resolution_clock::now()) begin;
// Print all prime numbers
for (int p=2; p<=n; p++)
{
if (flag)
{
begin=chrono::high_resolution_clock::now();
}
// ...
}
注意 全局 using namespace std
通常不是一个好主意。由于 begin
与 std::begin
冲突,错误消息变得复杂。如果改为写 namespace chrono = std::chrono; using std::cout;
,错误会变得更清楚:
test.cpp: In function ‘void SieveOfEratosthenes(int)’:
test.cpp:41:53: error: ‘begin’ was not declared in this scope; did you mean ‘std::begin’?
41 | auto duration=chrono::nanoseconds(end-begin);
| ^~~~~
| std::begin
#include <iostream>
#include <chrono>
#include <unistd.h>
using namespace std;
void SieveOfEratosthenes (int n)
{
bool prime[n+1], flag=true;
int counter=0, ct=0;
for (int i =0;i<n+1;i++){
prime[i]=true;
}
for (int p=2; p*p<=n; p++)
{
if (prime[p]==true)
{
for (int i=p*2; i<=n; i += p)
{
prime[i] = false;
}
}
}
// Print all prime numbers
for (int p=2; p<=n; p++)
{
if (flag)
{
auto begin=chrono::high_resolution_clock::now();
}
if (prime[p])
{
cout << p << " ";
counter+=1;
flag=false;
}
if(counter==10)
{
auto end=chrono::high_resolution_clock::now();
auto duration=chrono::nanoseconds(end-begin);
cout<<"Time elapsed:"<<duration.count();
counter=0;
flag=true;
}
}
cout<<endl;
}
int main()
{
int n;
cout<<"Type a number:";
cin>>n;
cout<<endl<<"Following are the prime numbers smaller or equal to:"<<n<<endl;
SieveOfEratosthenes(n);
return 0;
}
// Driver Program to test above function
这是一种查找小于 n 的素数(从键盘输入)的算法,我想得到每 10 个素数的时间 found.I 出现运算符错误并且它在(结束-开始)。我无法理解错误在哪里 is.And 我已经尝试将函数写在一个单独的 .o 文件中,但仍然得到 nothing.Any 帮助,将不胜感激。!
enter image description here
begin
定义在 if
块内,无法被 end-begin
访问。一个简单的解决方法是将定义移出 for
-loop:
decltype(chrono::high_resolution_clock::now()) begin;
// Print all prime numbers
for (int p=2; p<=n; p++)
{
if (flag)
{
begin=chrono::high_resolution_clock::now();
}
// ...
}
注意 全局 using namespace std
通常不是一个好主意。由于 begin
与 std::begin
冲突,错误消息变得复杂。如果改为写 namespace chrono = std::chrono; using std::cout;
,错误会变得更清楚:
test.cpp: In function ‘void SieveOfEratosthenes(int)’:
test.cpp:41:53: error: ‘begin’ was not declared in this scope; did you mean ‘std::begin’?
41 | auto duration=chrono::nanoseconds(end-begin);
| ^~~~~
| std::begin