Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
mgoullie
sp4_exercises
Commits
1af7a402
Unverified
Commit
1af7a402
authored
Oct 01, 2020
by
Guillaume Anciaux
Browse files
adding snippets of the day
parent
b64f6f3c
Changes
17
Hide whitespace changes
Inline
Side-by-side
lectures/code_snippets/array.cpp
0 → 100644
View file @
1af7a402
#include
<array>
#include
<string>
int
main
()
{
std
::
array
<
double
,
3
>
vec
;
vec
[
2
]
=
10.
;
std
::
array
<
std
::
string
,
10
>
vec2
;
vec2
[
8
]
=
"mars"
;
}
\ No newline at end of file
lectures/code_snippets/for_each.cpp
0 → 100644
View file @
1af7a402
#include
<algorithm>
#include
<iostream>
#include
<vector>
int
main
()
{
std
::
vector
<
int
>
vec
(
10
);
// indirect call
auto
foo
=
[](
auto
&
v
)
{
std
::
cout
<<
v
;
};
std
::
for_each
(
vec
.
begin
(),
vec
.
end
(),
foo
);
// inline call
std
::
for_each
(
vec
.
begin
(),
vec
.
end
(),
[](
auto
&
v
)
{
v
+=
1
;
});
// storing result in another array
std
::
vector
<
int
>
res
;
std
::
transform
(
vec
.
begin
(),
vec
.
end
(),
std
::
back_inserter
(
res
),
[](
auto
&
val
)
{
return
val
+
1
;
});
}
\ No newline at end of file
lectures/code_snippets/fstream.cpp
0 → 100644
View file @
1af7a402
#include
<cmath>
#include
<fstream>
#include
<iomanip>
#include
<iostream>
int
main
()
{
std
::
ofstream
fout
(
"test.plot"
);
fout
<<
M_PI
<<
std
::
endl
;
fout
.
close
();
double
pi_val
;
std
::
ifstream
fin
(
"test.plot"
);
fin
>>
pi_val
;
std
::
cout
<<
pi_val
<<
std
::
endl
;
fin
.
close
();
}
\ No newline at end of file
lectures/code_snippets/iostream.cpp
0 → 100644
View file @
1af7a402
#include
<iostream>
int
main
()
{
int
obj
;
std
::
cout
<<
obj
<<
std
::
endl
;
std
::
cout
<<
"flush now!"
<<
std
::
flush
<<
std
::
endl
;
std
::
cerr
<<
obj
<<
std
::
endl
;
}
\ No newline at end of file
lectures/code_snippets/lambda.cpp
0 → 100644
View file @
1af7a402
#include
<iostream>
void
foo1
()
{
std
::
cout
<<
"Hey"
<<
std
::
endl
;
}
int
main
()
{
// call a standard function
foo1
();
auto
foo2
=
[]()
{
std
::
cout
<<
"Hey again !"
<<
std
::
endl
;
};
foo2
();
auto
foo3
=
[](
int
a
,
double
b
)
{
std
::
cout
<<
a
<<
","
<<
b
<<
std
::
endl
;
};
foo3
(
10
,
102.3
);
int
toto
=
2
;
// captures 'toto' variable
auto
foo4
=
[
toto
]()
{
std
::
cout
<<
toto
<<
std
::
endl
;
};
foo4
();
// captures all in current scope
auto
foo5
=
[
&
]()
{
std
::
cout
<<
toto
<<
std
::
endl
;
};
foo5
();
// explicit declaration
auto
foo6
=
[]()
->
int
{
return
19
;
};
auto
res
=
foo6
();
// implicit declaration
auto
foo7
=
[]()
{
return
19.3
;
};
auto
res2
=
foo7
();
}
\ No newline at end of file
lectures/code_snippets/loops.cpp
0 → 100644
View file @
1af7a402
#include
<array>
#include
<vector>
int
main
()
{
std
::
vector
<
int
>
vec
(
10
);
for
(
int
i
=
0
;
i
<
10
;
++
i
)
{
vec
[
i
]
=
10
;
}
std
::
array
<
int
,
10
>
array
;
for
(
int
i
=
0
;
i
<
10
;
++
i
)
{
array
[
i
]
=
10
;
}
{
// vector loop with iterators
std
::
vector
<
int
>::
iterator
it
=
vec
.
begin
();
std
::
vector
<
int
>::
iterator
end
=
vec
.
end
();
for
(;
it
!=
end
;
++
it
)
{
*
it
=
10.
;
}
}
{
// with the auto
auto
it
=
vec
.
begin
();
auto
end
=
vec
.
end
();
for
(;
it
!=
end
;
++
it
)
{
*
it
=
10.
;
}
}
{
// with the range loop
for
(
auto
&
v
:
vec
)
{
v
=
10.
;
}
}
}
\ No newline at end of file
lectures/code_snippets/map.cpp
0 → 100644
View file @
1af7a402
#include
<map>
#include
<string>
int
main
()
{
std
::
map
<
std
::
string
,
double
>
particle_mass
;
particle_mass
[
"mars"
]
=
6.4171e23
;
particle_mass
[
"sun"
]
=
1.9885e30
;
particle_mass
[
"copper"
]
=
1.0552061e-25
;
}
\ No newline at end of file
lectures/code_snippets/new.cpp
0 → 100644
View file @
1af7a402
double
*
get_scalar
()
{
double
*
v
=
new
double
;
return
v
;
}
double
*
get_vector
(
int
n
)
{
double
*
v
=
new
double
[
n
];
return
v
;
}
int
main
()
{
double
*
scalar
=
get_scalar
();
// ... do what I need
delete
scalar
;
double
*
vector
=
get_vector
(
10
);
// ... do what I need
delete
[]
vector
;
}
\ No newline at end of file
lectures/code_snippets/scientific_precision.cpp
0 → 100644
View file @
1af7a402
#include
<iomanip>
#include
<iostream>
#include
<cmath>
int
main
(){
// for this output
std
::
cout
<<
std
::
scientific
<<
std
::
setprecision
(
15
)
<<
M_PI
<<
std
::
endl
;
//permanent
std
::
cout
.
precision
(
15
);
std
::
cout
.
setf
(
std
::
ios
::
scientific
);
}
\ No newline at end of file
lectures/code_snippets/set.cpp
0 → 100644
View file @
1af7a402
#include
<set>
int
main
()
{
std
::
set
<
int
>
set1
;
set1
.
insert
(
100
);
set1
.
insert
(
101
);
set1
.
insert
(
101
);
set1
.
erase
(
100
);
}
\ No newline at end of file
lectures/code_snippets/shared_pointers.cpp
0 → 100644
View file @
1af7a402
#include
<iostream>
#include
<memory>
std
::
shared_ptr
<
double
>
get_vector
(
int
n
)
{
return
std
::
shared_ptr
<
double
>
(
new
double
[
n
]);
}
int
main
()
{
std
::
shared_ptr
<
double
>
ptr1
=
get_vector
(
10
);
std
::
shared_ptr
<
double
>
ptr2
=
ptr1
;
// memory of pointer freed when ptr1 and ptr2 are out of scope
}
\ No newline at end of file
lectures/code_snippets/sstream.cpp
0 → 100644
View file @
1af7a402
#include
<iostream>
#include
<sstream>
int
main
()
{
std
::
stringstream
sstr
(
"1 2 3.14"
);
int
i
,
j
;
double
k
;
sstr
>>
i
>>
j
>>
k
;
std
::
cout
<<
i
<<
" "
<<
j
<<
" "
<<
k
<<
std
::
endl
;
std
::
stringstream
sstr2
;
sstr2
<<
142
;
std
::
cout
<<
sstr2
.
str
()
<<
std
::
endl
;
}
\ No newline at end of file
lectures/code_snippets/sstream_main_arguments.cpp
0 → 100644
View file @
1af7a402
#include
<sstream>
int
main
(
int
argc
,
char
**
argv
)
{
std
::
stringstream
sstr
;
for
(
int
i
=
1
;
i
<
argc
;
++
i
)
{
sstr
<<
argv
[
i
]
<<
" "
;
}
double
arg1
;
sstr
>>
arg1
;
int
arg2
;
sstr
>>
arg2
;
}
\ No newline at end of file
lectures/code_snippets/stream_error_management.cpp
0 → 100644
View file @
1af7a402
#include
<iostream>
#include
<sstream>
int
main
(
int
argc
,
char
**
argv
)
{
std
::
stringstream
sstr
(
"toto 2.5"
);
int
arg1
;
double
arg2
;
sstr
>>
arg1
>>
arg2
;
// good state if no error occurred
std
::
cout
<<
"good: "
<<
sstr
.
good
()
<<
std
::
endl
;
std
::
cout
<<
arg1
<<
" "
<<
arg2
<<
std
::
endl
;
}
\ No newline at end of file
lectures/code_snippets/string.cpp
0 → 100644
View file @
1af7a402
#include
<iostream>
#include
<string>
int
main
()
{
std
::
string
my_string
=
"test.plot"
;
std
::
cout
<<
my_string
<<
std
::
endl
;
}
\ No newline at end of file
lectures/code_snippets/unique_pointers.cpp
0 → 100644
View file @
1af7a402
#include
<iostream>
#include
<memory>
std
::
unique_ptr
<
double
>
get_scalar
()
{
// create a unique pointer
return
std
::
make_unique
<
double
>
(
3
);
}
int
main
()
{
std
::
unique_ptr
<
double
>
ptr
=
get_scalar
();
// ... do what I need like...
std
::
cout
<<
*
ptr
;
// no need to delete scalar (will be automatically)
// cannot be copied => compilation error
// std::unique_ptr<double> ptr_copy = ptr;
}
\ No newline at end of file
lectures/code_snippets/vector.cpp
0 → 100644
View file @
1af7a402
#include
<vector>
int
main
()
{
std
::
vector
<
double
>
vec
(
10
);
vec
.
resize
(
100
);
}
\ No newline at end of file
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment