Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
programming-in-cxx-2024
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
sdlbio-courses
programming-in-cxx-2024
Commits
9076d947
Commit
9076d947
authored
May 17, 2024
by
Sandipan Mohanty
Browse files
Options
Downloads
Patches
Plain Diff
Update chapter_08 examples
parent
8816b52f
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
chapter_08/examples/eratostenes_sieve.cc
+46
-0
46 additions, 0 deletions
chapter_08/examples/eratostenes_sieve.cc
chapter_08/examples/hello_m.cc
+2
-2
2 additions, 2 deletions
chapter_08/examples/hello_m.cc
with
48 additions
and
2 deletions
chapter_08/examples/eratostenes_sieve.cc
0 → 100644
+
46
−
0
View file @
9076d947
#include
<chrono>
#include
<ranges>
#include
<algorithm>
#include
<iostream>
#include
<vector>
#include
<span>
#include
<cxxopts.hpp>
namespace
sr
=
std
::
ranges
;
template
<
class
T
>
void
assign_each_nth
(
std
::
span
<
T
>
R
,
size_t
n
,
T
v
)
{
for
(
auto
i
=
0UL
;
i
<
R
.
size
();
i
+=
n
)
R
[
i
]
=
v
;
}
auto
main
(
int
argc
,
char
*
argv
[])
->
int
{
std
::
cout
<<
"Erastothene Sieve...
\n
"
;
using
namespace
std
;
using
namespace
std
::
chrono
;
auto
N
=
100UL
;
vector
<
int
>
candidate
(
N
+
1
,
1
);
candidate
[
0
]
=
candidate
[
1
]
=
0
;
auto
proc
=
std
::
span
(
candidate
);
auto
t0
=
steady_clock
::
now
();
for
(
size_t
i
=
2
;
i
<=
(
N
/
2
);
++
i
)
{
if
(
candidate
[
i
])
{
assign_each_nth
(
proc
.
subspan
(
2
*
i
),
i
,
0
);
}
}
auto
t1
=
steady_clock
::
now
();
std
::
cout
<<
"Calculating primes up to "
<<
N
<<
" took "
<<
duration
<
double
>
(
t1
-
t0
)
<<
"
\n
"
;
std
::
cout
<<
"Write primes ? (y/n): "
;
char
ch
;
std
::
cin
>>
ch
;
if
(
ch
==
'y'
)
for
(
size_t
i
=
0
;
i
<
N
;
++
i
)
if
(
candidate
[
i
])
std
::
cout
<<
i
<<
"
\n
"
;
std
::
cout
<<
"Prime count = "
<<
sr
::
count
(
candidate
,
1
)
<<
"
\n
"
;
}
This diff is collapsed.
Click to expand it.
chapter_08/examples/hello_m.cc
+
2
−
2
View file @
9076d947
import
<
iostream
>
;
import
<
print
>
;
auto
main
()
->
int
auto
main
()
->
int
{
{
std
::
cout
<<
"Hello, world!
\n
"
;
std
::
print
(
"Hello, world!
\n
"
)
;
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment