# include <iostream>
# include <memory>
# include <string>
int main ( int argc, char const * argv[ ] )
{
char * p = "this is const string" ;
char pStr[ ] = "this is test" ;
std:: auto_ptr< std:: string> pAuto ( new std:: string ( "hello" ) ) ;
std:: unique_ptr< std:: string> pUnique ( new std:: string ( "hellow" ) ) ;
uint32_t * pInt = reinterpret_cast < uint32_t * > ( new int ( 10 ) ) ;
int * pTmpInt = new int ( 10 ) ;
return 0 ;
}
# include <iostream>
# include <stdio.h>
constexpr int fibonacci ( const int n)
{
printf ( "%d\n" , n) ;
if ( n == 1 ) return 1 ;
if ( n == 2 ) return 1 ;
return fibonacci ( n- 1 ) + fibonacci ( n- 2 ) ;
}
int main ( int argc, const char * * argv)
{
int a[ fibonacci ( 5 ) ] = { 0 } ;
int b[ fibonacci ( 5 ) ] = { 0 } ;
return 0 ;
}
# include <iostream>
# include <type_traits>
void foo ( char * ) {
std:: cout << "foo(char*) is called" << std:: endl;
}
void foo ( int i) {
std:: cout << "foo(int) is called" << std:: endl;
}
constexpr int fibonacii ( const int n)
{
std:: cout << n << std:: endl;
return ( 1 == n || 2 == n) ? 1 : ( fibonacii ( n - 1 ) + fibonacii ( n - 2 ) ) ;
}
int main ( int argc, const char * * argv)
{
if ( std:: is_same< decltype ( NULL ) , decltype ( 0 ) > :: value)
{
std:: cout << "NULL == 0" << std:: endl;
}
if ( std:: is_same< decltype ( NULL ) , decltype ( ( void * ) 0 ) > :: value)
std:: cout << "NULL == (void *)0" << std:: endl;
if ( std:: is_same< decltype ( NULL ) , std:: nullptr_t> :: value)
std:: cout << "NULL == nullptr" << std:: endl;
foo ( 0 ) ;
foo ( nullptr ) ;
int a[ fibonacii ( 5 ) ] = { 0 } ;
return 0 ;
}
# include <iostream>
# include <algorithm>
# include <vector>
using namespace std;
int main ( )
{
std:: vector< int > vctNums ( { 1 , 2 , 3 , 4 } ) ;
auto it2 = std:: find ( vctNums. begin ( ) , vctNums. end ( ) , 2 ) ;
if ( vctNums. end ( ) != it2)
{
* it2 = 4 ;
}
for_each ( vctNums. begin ( ) , vctNums. end ( ) , [ ] ( int n) {
std:: cout << n << std:: endl;
} ) ;
std:: cout << "------use c++17--------" << std:: endl;
if ( auto it = std:: find ( vctNums. begin ( ) , vctNums. end ( ) , 3 ) ; it != vctNums. end ( ) )
{
* it = 99 ;
}
std:: for_each ( vctNums. begin ( ) , vctNums. end ( ) , [ ] ( int n) {
std:: cout << n << std:: endl;
} ) ;
return 0 ;
}
# include <iostream>
# include <algorithm>
# include <initializer_list>
# include <vector>
using namespace std;
class MyClass
{
private :
std:: vector< int > _m_vctNumber;
public :
MyClass ( std:: initializer_list< int > list)
{
for ( auto & item : list)
{
_m_vctNumber. push_back ( item) ;
}
}
void Show ( )
{
for ( const auto & item: _m_vctNumber)
{
std:: cout << item << std:: endl;
}
}
} ;
int main ( )
{
MyClass mcl ( { 1 , 4 , 9 } ) ;
mcl. Show ( ) ;
return 0 ;
}
# include <iostream>
# include <algorithm>
# include <tuple>
using namespace std;
std:: tuple< int , double , std:: string> f ( ) {
return std:: make_tuple ( 1 , 2.3 , "456" ) ;
}
int main ( )
{
auto [ x, y, z] = std:: make_tuple ( 1 , "sddd" , 3.999 ) ;
std:: cout << x << ", " << y << ", " << z << std:: endl;
return 0 ;
}
# include <iostream>
# include <algorithm>
using namespace std;
template < typename Arg1 , typename Arg2 >
auto add ( Arg1 arg1, Arg2 arg2) -> decltype ( arg1 + arg2)
{
return arg1 + arg2;
}
template < typename Arg1 , typename Arg2 >
auto add_cpp17 ( Arg1 arg1, Arg2 arg2)
{
return arg1 + arg2;
}
template < typename T = int , typename U = int >
auto add_default ( T x, U y) -> decltype ( x+ y) {
return x+ y;
}
int main ( )
{
auto r = add< int , double > ( 1 , 1.234 ) ;
auto r2 = add_cpp17< int , double > ( 1 , 1.234 ) ;
auto r3 = add_default ( 9 , 20 ) ;
std:: cout << r << std:: endl;
std:: cout << r2 << std:: endl;
std:: cout << r3 << std:: endl;
return 0 ;
}
# include <iostream>
# include <algorithm>
using namespace std;
template < typename T >
void PrintTypeInfo ( T arg)
{
if constexpr ( std:: is_integral< T> :: value ) {
re
}
}
int main ( )
{
return 0 ;
}
# include <iostream>
# include <algorithm>
# include <vector>
using namespace std;
typedef int ( * cbfunc) ( int , int ) ;
using cbfuncex = int ( * ) ( int , int ) ;
using VCTINT = std:: vector< int > ;
int foo ( int a, int b)
{
return a + b;
}
void run ( cbfunc func , int a, int b)
{
std:: cout << func ( a, b) << std:: endl;
}
int main ( )
{
cbfunc cb = foo;
run ( cb, 9 , 10 ) ;
cbfuncex cbex = foo;
run ( cbex, 10 , 29 ) ;
VCTINT a = { 9 , 2 , 39 , 10 } ;
for_each ( a. begin ( ) , a. end ( ) , [ ] ( int n) {
std:: cout << n << std:: endl;
} ) ;
return 0 ;
}
# include <iostream>
# include <algorithm>
# include <initializer_list>
using namespace std;
template < typename T >
void myprintf ( T value)
{
std:: cout << value << std:: endl;
}
template < typename T , typename . . . Args>
void myprintf ( T value, Args. . . args)
{
std:: cout << value << std:: endl;
myprintf ( args. . . ) ;
}
template < typename T , typename . . . Args>
void myprintf2 ( T value, Args. . . args)
{
std:: cout << value << std:: endl;
if constexpr ( sizeof . . . ( args) > 0 ) {
myprintf2 ( args. . . ) ;
}
}
template < typename T , typename . . . Args>
auto myprintf3 ( T value, Args. . . args)
{
std:: cout << value << std:: endl;
auto r = std:: initializer_list< T> { ( [ & args] {
std:: cout << args << std:: endl;
} ( ) , value) . . . } ;
std:: cout << "size is : " << r. size ( ) << std:: endl;
std:: cout << "initializer_list is :" << std:: endl;
for_each ( r. begin ( ) , r. end ( ) , [ ] ( T v) {
std:: cout << v << std:: endl;
} ) ;
}
template < typename . . . T>
auto Sum ( T . . . args)
{
return ( args + . . . ) ;
}
template < typename T , int nSize>
class MyBuf
{
public :
virtual ~ MyBuf ( ) { }
private :
T _m_data[ nSize] ;
} ;
int main ( )
{
myprintf ( 1 , 2 , "666" , 234.9234 , 'c' , true ) ;
std:: cout << "--------" << std:: endl;
myprintf2 ( 1 , 2 , "666" , 234.9234 , 'c' , true ) ;
std:: cout << "--------" << std:: endl;
myprintf3 ( 1 , 2 , "666" , 234.9234 , 'c' , true ) ;
std:: cout << "--------" << std:: endl;
std:: cout << "sum : " << Sum ( 9 , 1 , 99 , 1.123 ) << std:: endl;
MyBuf< int , 100 > buf;
std:: cout << "sizeof(buf) is " << sizeof ( buf) << std:: endl;
std:: cout << "sizeof(char *) is " << sizeof ( char * ) << std:: endl;
std:: cout << "sizeof(int): " << sizeof ( int ) << std:: endl;
std:: cout << "sizeof(short): " << sizeof ( short ) << std:: endl;
std:: cout << "sizeof(long): " << sizeof ( long ) << std:: endl;
std:: cout << "sizeof(long long): " << sizeof ( long long ) << std:: endl;
std:: cout << "sizeof(float): " << sizeof ( float ) << std:: endl;
std:: cout << "sizeof(double): " << sizeof ( double ) << std:: endl;
return 0 ;
}
# include <iostream>
# include <algorithm>
# include <string>
using namespace std;
enum class RGB : unsigned int {
RED = 0x0 ,
GREEN = 0xff ,
BLUE = 0xffff ,
} ;
enum class HHH {
RED = 0x0 ,
GREEN = 0xff ,
BLUE = 0xffff ,
} ;
enum class MAP : int {
FIRST = - 1 ,
SECOND = - 999 ,
THRID = 19900
} ;
template < typename T >
std:: ostream& operator << ( typename std :: enable_if< std:: is_enum< T> :: value, std:: ostream> :: type& stream, const T& e)
{
return stream << static_cast < typename std :: underlying_type< T> :: type> ( e) ;
}
int main ( )
{
if ( RGB:: RED == RGB:: BLUE)
{
std:: cout << "RGB::RED == RGB::BLUE" << std:: endl;
}
std:: cout << MAP:: THRID << std:: endl;
return 0 ;
}
# include <iostream>
# include <algorithm>
using namespace std;
class Base
{
public :
int _m_v1;
int _m_v2;
Base ( )
{
_m_v1 = 1 ;
}
Base ( int n) : Base ( )
{
_m_v2 = n;
}
virtual void FinalFunc ( ) final
{
std:: cout << "this is final function" << std:: endl;
}
} ;
class Sub : public Base
{
public :
int _m_v3;
Sub ( int v3) : Base ( 2 )
{
_m_v3 = v3;
}
public :
using Base:: Base;
} ;
class MyClass
{
public :
MyClass ( ) = default ;
MyClass& operator = ( const MyClass& ) = delete ;
MyClass ( int n) { std:: cout << n << std:: endl; } ;
} ;
int main ( )
{
Base b ( 9 ) ;
std:: cout << b. _m_v1 << std:: endl;
std:: cout << b. _m_v2 << std:: endl;
Sub s ( 9 ) ;
std:: cout << " ---------" << std:: endl;
std:: cout << s. _m_v1 << std:: endl;
std:: cout << s. _m_v2 << std:: endl;
std:: cout << s. _m_v3 << std:: endl;
std:: cout << " ---------" << std:: endl;
MyClass mcls ( 99 ) ;
return 0 ;
}
# include <iostream>
template < typename . . . T>
auto average ( T . . . t) {
return ( t + . . . ) / sizeof . . . ( t) ;
}
int main ( ) {
std:: cout << average ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ) << std:: endl;
}
# include <iostream>
# include <map>
# include <string>
# include <functional>
template < typename Key , typename Value , typename F >
void update ( std:: map< Key, Value> & m, F foo) {
for ( auto && [ key, value] : m ) value = foo ( key) ;
}
int main ( ) {
std:: map< std:: string, long long int > m {
{ "a" , 1 } ,
{ "b" , 2 } ,
{ "c" , 3 }
} ;
update ( m, [ ] ( std:: string key) -> long long int {
return std:: hash< std:: string> { } ( key) ;
} ) ;
for ( auto && [ key, value] : m)
std:: cout << key << ":" << value << std:: endl;
}
# include <iostream>
# include <algorithm>
# include <memory>
using namespace std;
int main ( )
{
int value = 1 ;
auto foo = [ = ] {
return value;
} ;
value = 9 ;
auto value2 = foo ( ) ;
std:: cout << "value = " << value << std:: endl;
std:: cout << "value2 = " << value2 << std:: endl;
int a = 0 ;
auto func = [ a] ( int v) {
v = a + 1 ;
return v;
} ;
auto b = func ( a) ;
auto c = a;
std:: cout << "b = " << b << std:: endl;
std:: cout << "c = " << c << std:: endl;
int x = 0 ;
auto fun = [ & x] ( int n) -> int & {
x += 1 + n;
return x;
} ;
x++ ;
auto y = fun ( x) ;
auto & z = fun ( x) ;
y++ ;
z++ ;
std:: cout << "x = " << x << std:: endl;
std:: cout << "y = " << y << std:: endl;
std:: cout << "z = " << z << std:: endl;
auto ptr = std:: make_unique< int > ( 9 ) ;
auto add = [ v1 = 1 , v2 = std:: move ( ptr) ] ( int x, int y) {
return x + y + v1 + ( * v2) ;
} ;
std:: cout << add ( 9 , 10 ) << std:: endl;
auto f = [ ] ( auto x, auto y) -> auto {
return x + y;
} ;
std:: cout << f ( 9 , 2 ) << std:: endl;
return 0 ;
}
# include <iostream>
# include <algorithm>
# include <functional>
using namespace std;
int foo ( int n)
{
return n;
}
void increase ( int & v) {
v++ ;
}
int main ( )
{
std:: function< int ( int ) > f = foo;
std:: cout << f ( 9 ) << std:: endl;
std:: function< int ( int ) > func = [ d = 9 ] ( int value) -> int {
return d + value;
} ;
std:: cout << func ( 9 ) << std:: endl;
auto add = [ ] ( auto a, auto b, auto c) -> auto {
return a + b + c;
} ;
auto bf = std:: bind ( add, std:: placeholders:: _1, 100 , 200 ) ;
std:: cout << bf ( 80 ) << std:: endl;
return 0 ;
}
# include <iostream>
# include <algorithm>
# include <string>
using namespace std;
class MyClass
{
private :
int * _m_pData;
std:: string _m_strName;
public :
MyClass ( const std:: string & strName)
: _m_pData ( new int ( 10 ) ) , _m_strName ( strName)
{
std:: cout << "construct " << _m_strName << std:: endl;
}
MyClass ( MyClass& rhs) :
_m_pData ( new int ( * ( rhs. _m_pData) ) ) ,
_m_strName ( rhs. _m_strName)
{
std:: cout << "copy " << rhs. _m_strName << std:: endl;
}
MyClass ( MyClass&& r_rhs) :
_m_strName ( r_rhs. _m_strName) ,
_m_pData ( r_rhs. _m_pData)
{
std:: cout << " moved " << r_rhs. _m_strName << std:: endl;
r_rhs. _m_pData = nullptr ;
r_rhs. _m_strName. clear ( ) ;
}
~ MyClass ( )
{
std:: cout << _m_strName << " ~MyClass() " << std:: endl;
delete _m_pData;
}
} ;
MyClass foo ( bool flag)
{
MyClass objA ( "A" ) , objB ( "B" ) ;
if ( flag) {
std:: cout << "a" << std:: endl;
return objA;
}
else {
std:: cout << "b" << std:: endl;
return objB;
}
}
int main ( )
{
MyClass a = foo ( true ) ;
std:: cout << "obj: " << std:: endl;
return 0 ;
}
# include <iostream>
# include <algorithm>
# include <array>
# include <list>
# include <forward_list>
using namespace std;
int main ( )
{
std:: array< int , 4 > arr = { 1 , 2 , 4 , 5 } ;
if ( arr. empty ( ) )
{
std:: cout << "is empty" << std:: endl;
}
std:: cout << "size : " << arr. size ( ) << std:: endl;
for ( auto & item : arr)
{
std:: cout << item << std:: endl;
}
std:: forward_list< std:: string> flstStrings;
flstStrings. push_front ( "hello1" ) ;
flstStrings. push_front ( "hello2" ) ;
flstStrings. push_front ( "hello3" ) ;
flstStrings. push_front ( "hello4" ) ;
for ( auto item : flstStrings)
{
std:: cout << item << std:: endl;
}
std:: sort ( arr. begin ( ) , arr. end ( ) , [ ] ( int a, int b) -> bool {
return a > b;
} ) ;
std:: for_each ( arr. begin ( ) , arr. end ( ) , [ ] ( int n) {
std:: cout << n << " \t " ;
} ) ;
std:: cout << std:: endl;
return 0 ;
}
# include <iostream>
# include <algorithm>
# include <unordered_map>
# include <unordered_set>
using namespace std;
int main ( )
{
std:: unordered_map< std:: string, std:: string> mapInfo;
mapInfo. insert ( std:: make_pair ( "boy1" , "joke" ) ) ;
mapInfo. insert ( std:: make_pair ( "boy2" , "Kobe" ) ) ;
mapInfo. insert ( std:: make_pair ( "boy3" , "Map" ) ) ;
std:: for_each ( mapInfo. begin ( ) , mapInfo. end ( ) , [ ] ( auto item) {
std:: cout << item. first << " : " << item. second << std:: endl;
} ) ;
std:: unordered_multimap< std:: string, std:: string> mapMulMap;
mapMulMap. insert ( std:: make_pair ( "boy1" , "joke" ) ) ;
mapMulMap. insert ( std:: make_pair ( "boy1" , "Kobe" ) ) ;
mapMulMap. insert ( std:: make_pair ( "boy1" , "Map" ) ) ;
std:: for_each ( mapMulMap. begin ( ) , mapMulMap. end ( ) , [ ] ( auto item) {
std:: cout << item. first << " : " << item. second << std:: endl;
} ) ;
return 0 ;
}
# include <iostream>
# include <algorithm>
# include <any>
# include <string>
using namespace std;
int main ( )
{
std:: any a = 9 ;
std:: cout << std:: any_cast< int > ( a) << std:: endl;
a = std:: string ( "this is a" ) ;
std:: cout << std:: any_cast< std:: string> ( a) << std:: endl;
a. reset ( ) ;
if ( a. has_value ( ) )
{
std:: cout << "a 中有值" << std:: endl;
}
else
{
std:: cout << "a 中无值" << std:: endl;
}
return 0 ;
}
# include <iostream>
# include <algorithm>
# include <tuple>
# include <variant>
using namespace std;
template < size_t n, typename . . . T>
constexpr std:: variant< T. . . > _tuple_index ( const std:: tuple< T. . . > & tpl, size_t i)
{
if constexpr ( n >= sizeof . . . ( T) )
throw std:: out_of_range ( " 越界." ) ;
if ( i == n)
return std:: variant< T. . . > { std:: in_place_index< n> , std:: get< n> ( tpl) } ;
return _tuple_index< ( n < sizeof . . . ( T) - 1 ? n + 1 : 0 ) > ( tpl, i) ;
}
template < typename . . . T>
constexpr std:: variant< T. . . > tuple_index ( const std:: tuple< T. . . > & tpl, size_t i)
{
return _tuple_index< 0 > ( tpl, i) ;
}
template < typename T0 , typename . . . Ts>
std:: ostream & operator << ( std:: ostream & s, std:: variant< T0, Ts. . . > const & v)
{
std:: visit ( [ & ] ( auto && x) { s << x; } , v) ;
return s;
}
int main ( int argc, char * argv[ ] )
{
std:: variant< float , double , std:: string, int > vars;
vars = 9 ;
std:: visit ( [ & ] ( auto && x) { std:: cout << x << std:: endl; } , vars ) ;
std:: tuple< std:: string, double , double , int > t ( "123" , 4.5 , 6.7 , 8 ) ;
int i = argc;
std:: cout << tuple_index ( t, i) << std:: endl;
return 0 ;
}
# include <iostream>
# include <algorithm>
# include <variant>
# include <valarray>
# include <iomanip>
using namespace std;
union MyUnion
{
int i;
float f;
char ch;
} ;
using Two = std:: pair< double , double > ;
using Roots = std:: variant< Two, double > ;
Roots FindRoots ( double a, double b, double c)
{
auto d = b* b- 4 * a* c;
if ( d > 0.0 )
{
auto p = sqrt ( d) ;
return std:: make_pair ( ( - b + p) / 2 * a, ( - b - p) / 2 * a) ;
}
else if ( d == 0.0 )
return ( - 1 * b) / ( 2 * a) ;
return 0.0 ;
}
struct RootPrinterVisitor
{
void operator ( ) ( const Two& roots) const
{
std:: cout << "2 roots: " << roots. first << " " << roots. second << '\n' ;
}
void operator ( ) ( double root) const
{
std:: cout << "1 root: " << root << '\n' ;
}
void operator ( ) ( void * ) const
{
std:: cout << "No real roots found.\n" ;
}
} ;
int main ( )
{
double pi = 3.14159265359 ;
const int nSize = 10 ;
std:: valarray< double > vctFloats ( nSize) ;
for ( int i = 0 ; i < nSize; i++ )
{
vctFloats[ i] = 0.25 * i - 1 ;
}
for ( int i = 0 ; i < nSize; i++ )
{
std:: cout << " " << vctFloats[ i] ;
}
std:: cout << std:: endl;
std:: valarray< double > vctFloats2 ( nSize) ;
vctFloats2 = acos ( vctFloats) ;
for ( int i = 0 ; i < nSize ; i++ )
{
cout << setw ( 10 ) << vctFloats2 [ i ]
<< " radians, which is "
<< setw ( 11 ) << ( 180 / pi) * vctFloats2 [ i ]
<< " degrees" << endl;
}
std:: variant< int , float , std:: string> var;
var = std:: string ( "hello" ) ;
std:: cout << std:: get< std:: string> ( var) << std:: endl;
var = ( float ) 0.9999 ;
std:: cout << std:: get< float > ( var) << std:: endl;
var = ( int ) 999 ;
std:: cout << std:: get< int > ( var) << std:: endl;
MyUnion u;
std:: cout << "sizeof(MyUnion) = " << sizeof ( MyUnion) << std:: endl;
u. f = 999.999 ;
std:: cout << u. f << std:: endl;
u. i = 999 ;
std:: cout << u. i << std:: endl;
u. ch = 'z' ;
std:: cout << u. ch << std:: endl;
std:: cout << u. f<< std:: endl;
std:: cout << u. i<< std:: endl;
return 0 ;
}
# include <iostream>
# include <algorithm>
# include <string>
# include <memory>
using namespace std;
void foo ( std:: shared_ptr< std:: string> & sp)
{
* sp += std:: string ( "good" ) ;
}
struct A ;
struct B ;
struct A
{
std:: shared_ptr< B> pointer;
A ( ) { std:: cout << "A 构造" << std:: endl; }
~ A ( )
{
std:: cout << "A 被销毁" << std:: endl;
}
} ;
struct B
{
std:: shared_ptr< A> pointer;
B ( ) { std:: cout << "B 构造" << std:: endl; }
~ B ( )
{
std:: cout << "B 被销毁" << std:: endl;
}
} ;
int main ( )
{
std:: shared_ptr< std:: string> sp = std:: make_shared< std:: string> ( "hello" ) ;
std:: cout << * sp << std:: endl;
foo ( sp) ;
std:: cout << * sp << std:: endl;
std:: unique_ptr< std:: string> up = std:: make_unique< std:: string> ( "hello" ) ;
auto a = std:: make_shared< A> ( ) ;
auto b = std:: make_shared< B> ( ) ;
a-> pointer = b;
b-> pointer = a;
return 0 ;
}
# include <iostream>
# include <algorithm>
# include <string>
# include <regex>
using namespace std;
int main ( )
{
std:: string fnames[ ] = { "foo.txt" , "bar.txt" , "test" , "a0.txt" , "AAA.txt" } ;
std:: regex txt_regex ( R"([a-z]+\.txt)" ) ;
for ( const auto & fname: fnames)
{
std:: cout << fname << ": " << std:: regex_match ( fname, txt_regex) << std:: endl;
}
return 0 ;
}
# include <iostream>
# include <algorithm>
# include <string>
# include <future>
# include <thread>
# include <cmath>
# include <chrono>
using namespace std;
int gcd ( int a, int b) {
int temp;
if ( a < b) {
temp = a;
a = b;
b = temp;
}
while ( b != 0 ) {
temp = a % b;
a = b;
b = temp;
}
return a;
}
int main ( )
{
std:: packaged_task< double ( double ) > task ( [ ] ( double n) -> double {
std:: this_thread:: sleep_for ( std:: chrono:: seconds ( 10 ) ) ;
return sqrt ( n) ;
} ) ;
std:: packaged_task< int ( int , int ) > gcdTask ( gcd) ;
std:: future< double > result = task. get_future ( ) ;
std:: future< int > gcdResult = gcdTask. get_future ( ) ;
std:: thread ( std:: move ( gcdTask) , 23782349 , 7282335 ) . detach ( ) ;
std:: thread ( std:: move ( task) , 9.9234 ) . detach ( ) ;
std:: cout << "do othering" << std:: endl;
for ( int i = 0 ; i < 100 ; i++ )
{
double sum = i * 3.1234234329 ;
std:: cout << sum << std:: endl;
}
std:: cout << "gcd result: " << gcdResult. get ( ) << std:: endl;
std:: cout << "get result" << std:: endl;
if ( result. valid ( ) )
{
std:: cout << "result is valid" << std:: endl;
std:: cout << "result is :" << result. get ( ) << std:: endl;
}
else
{
std:: cout << "result is invalid" << std:: endl;
}
return 0 ;
}
# include <iostream>
# include <algorithm>
# include <string>
# include <thread>
# include <future>
# include <vector>
# include <cmath>
using namespace std;
std:: vector< uint64_t > PrimeFacotrization ( uint64_t n)
{
std:: vector< uint64_t > vctFactors;
for ( uint64_t i = 2 ; i <= sqrt ( n) ; i++ )
{
if ( n % i == 0 )
{
vctFactors. push_back ( i) ;
vctFactors. push_back ( n / i ) ;
}
}
return std:: move ( vctFactors) ;
}
int main ( int argc, char * argv[ ] )
{
if ( argc < 2 )
{
std:: cout << "usage ./a.out number" << std:: endl;
return 0 ;
}
uint64_t num = atoll ( argv[ 1 ] ) ;
std:: future< std:: vector< uint64_t > > result = std:: async ( PrimeFacotrization, num) ;
PrimeFacotrization ( num * 2 ) ;
std:: cout << "开始获取 aysnc函数的返回值" << std:: endl;
auto factors = result. get ( ) ;
std:: for_each ( factors. begin ( ) , factors. end ( ) , [ ] ( uint64_t n ) {
std:: cout << n << " " ;
} ) ;
std:: cout << std:: endl;
return 0 ;
}
# include <iostream>
# include <algorithm>
# include <string>
# include <boost/lockfree/spsc_queue.hpp>
# include <boost/thread/thread.hpp>
# include <boost/container/vector.hpp>
# include <boost/thread/future.hpp>
# include <boost/random.hpp>
# include <boost/move/move.hpp>
# include <boost/container/deque.hpp>
# include <boost/thread/mutex.hpp>
using namespace std;
int main ( )
{
using boost:: thread;
using boost:: container:: vector;
using boost:: lockfree:: spsc_queue;
spsc_queue< double > lockfreeQueue ( 100000000 ) ;
const int cnProducer = 10 , cnConsumer = 10 ;
boost:: mutex mtx;
vector< thread> vctProducer;
const double pi = 3.14159261314 ;
for ( int i = 0 ; i < cnProducer; i++ )
{
vctProducer. push_back ( boost:: move ( thread ( [ & pi, & lockfreeQueue, & mtx] ( ) {
for ( int i = 0 ; i < 10000000 ; i++ )
{
lockfreeQueue. push ( pow ( i, 3 ) * pi / 2 ) ;
}
} ) ) ) ;
}
for ( auto & thd : vctProducer)
{
thd. join ( ) ;
}
return 0 ;
}
# include <boost/thread/thread.hpp>
# include <iostream>
void hello ( )
{
std:: cout << "Hello world, I''m a thread!" << std:: endl;
}
int main ( int argc, char * argv[ ] )
{
boost:: thread thrd ( & hello) ;
thrd. join ( ) ;
return 0 ;
}