c++11

·

#define _SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING
#include<iostream>
#include <array>
#include<string>
#include <functional>
using namespace std;
void fun(array<int,10> &a)
{
int i = 0;
for (auto element…


This content originally appeared on DEV Community and was authored by 海前 王

·

#define _SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING 
#include<iostream>
#include <array>
#include<string>
#include <functional>
using namespace std;
void fun(array<int,10> &a)
{
    int i = 0;
    for (auto element : a)
    {
        a[i++] += 1;
        cout << element;
    }
}

int main()
{

    array<int,10> a{1,2,3};
    a.front() = 4;
    a.operator[](1) = 9;
    fun(std::ref(a));
    for (auto a1 : a)
    {
        cout << a1;
    }
    string f = "hello";
    //for(auto i:f)
    //{
    //  cout << i;
    //}
    for (char value : f)
    {
        value += 1;
        cout << value;
    }
//  logical_or<>();
    logical_or<int> result;

    int ch[10];
    try
    {
        for (int& ch1 : ch)
        {
            cin >> ch1;
        }
    }
    catch (const exception& e)
    {
        cout << e.what();
    }   //}
    for (int ch1 : ch)
    {
        cout << ch1;
    }


}
int mian()
{

    return 0;
}


// ConsoleApplication1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//#include <iostream>
//#include <type_traits>
//
//template <class T>
//struct Number { T n; };
//
//template <class T, class U>
//Number<typename std::common_type<T, U>::type> operator+(const Number<T>& lhs,
//    const Number<U>& rhs)
//{
//    return { lhs.n + rhs.n };
//}
//
//int main()
//{
//    Number<int> i1 = { 1 }, i2 = { 2 };
//    Number<double> d1 = { 2.3 }, d2 = { 3.5 };
//    std::cout << "i1i2: " << (i1 + i2).n << "\ni1d2: " << (i1 + d2).n << '\n'
//        << "d1i2: " << (d1 + i2).n << "\nd1d2: " << (d1 + d2).n << '\n';
//}
// Compile using cl.exe /EHsc
// common_type sample
#include <iostream>
#include <type_traits>

struct Base {};
struct Derived : Base {};

int main()
{
    typedef std::common_type<unsigned char, short, int>::type NumericType;
    typedef std::common_type<float, double>::type FloatType;
    typedef std::common_type<const int, volatile int>::type ModifiedIntType;
    typedef std::common_type<Base, Derived>::type ClassType;

    std::cout << std::boolalpha;
    std::cout << "Test for typedefs of common_type int" << std::endl;
    std::cout << "NumericType: " << std::is_same<int, NumericType>::value << std::endl;
    std::cout << "FloatType: " << std::is_same<int, FloatType>::value << std::endl;
    std::cout << "ModifiedIntType: " << std::is_same<int, ModifiedIntType>::value << std::endl;
    std::cout << "ClassType: " << std::is_same<int, ClassType>::value << std::endl;
    std::cout << "---------------------------" << std::endl;
    std::cout << "Test for typedefs of common_type double" << std::endl;
    std::cout << "NumericType: " << std::is_same<double, NumericType>::value << std::endl;
    std::cout << "FloatType: " << std::is_same<double, FloatType>::value << std::endl;
    std::cout << "ModifiedIntType: " << std::is_same<double, ModifiedIntType>::value << std::endl;
    std::cout << "ClassType: " << std::is_same<double, ClassType>::value << std::endl;
    std::cout << "---------------------------" << std::endl;
    std::cout << "Test for typedefs of common_type Base" << std::endl;
    std::cout << "NumericType: " << std::is_same<Base, NumericType>::value << std::endl;
    std::cout << "FloatType: " << std::is_same<Base, FloatType>::value << std::endl;
    std::cout << "ModifiedIntType: " << std::is_same<Base, ModifiedIntType>::value << std::endl;
    std::cout << "ClassType: " << std::is_same<Base, ClassType>::value << std::endl;

    return 0;
}

//// ConsoleApplication2.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
////
//#include <functional>
//#include <vector>
//#include <iostream>
//template<typename T>
//void foo(T val)
//{
//
//std:: cout << val;
//
//}
//
//
//
//
//
//int main()
//{
//  void fun(int x, int y);
//  std::vector<std::function<void(int, int)>> ta;
//  ta.push_back(fun);
//  ta.push_back([](int x, int y)
//      {
//          std::cout << x;
//      });
//  for(std::function<void(int,int)> f:ta)
//  {
//      f(33, 45);
//  }
//  int x{3};
//  foo(std::ref(x));
//}

#include <iostream>
#include <functional>
#include <vector>
#include <algorithm>
void fun(int x, int y)
{
    std::cout << "fun: " << x << ", " << y << std::endl;
}
bool int_ptr_less(int *a,int *b)
{
    return *a  < *b;

}
class c
{
public:
    void mfunc(int x, int y) const
    {
        std::cout << "fun: " << x << ", " << y << std::endl;
    }
};

auto divide(int a, int b) -> decltype((double)a/b) {
    if (b != 0) {
        return static_cast<double>(a) / b;
    }
    else {
        throw std::invalid_argument("Division by zero");
    }
}

int main()
{
    std::vector<std::function<void(int, int)>> ta;
    ta.push_back(fun);
    ta.push_back([](int x, int y)
    {
        std::cout << "Lambda: " << x << y << std::endl;
    });

    for (std::function<void(int, int)> f : ta)
    {
        f(33, 45);
    }

    std::function<void(const c&, int, int)> mf;
    mf = &c::mfunc;
    mf(c(), 34, 34);


    int x = 17;
    int y = 42;
    int z = 33;
    int* px = &x;
    int* py = &y;
    int* pz = &z;
    int* pmax = std::max(px, py, int_ptr_less);


    std::pair<int*, int*> ex = std::minmax({ px,py,pz }, int_ptr_less);//first 17 second 42


    auto ex1 = std::minmax({ px,py,pz }, [](int*a,int*b)
    {
            return *a > *b;
    });

    int bz = 10;
    decltype(bz) fz = 10;
    std::cout << fz;
    auto i=divide(32, 4);
   return 0;
}
// ConsoleApplication3.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>  
#include <thread>  
#include <atomic>  
#include <chrono>  

std::atomic<bool> ready(false); // 初始化为 false 的原子布尔变量  

void workerThread() {
    std::this_thread::sleep_for(std::chrono::seconds(1));
    ready.store(true); // 设置 ready 为 true,通知其他线程  
    std::cout << "Worker thread finished its work.\n";
}

int main() {
    std::thread t(workerThread);
    t.detach();
    while (!ready.load()) {
        std::cout << "Waiting for the worker thread to finish...\n";
        std::this_thread::sleep_for(std::chrono::milliseconds(100)); // 等待 worker 线程完成  
    }

    std::cout << "Main thread detected that the work is ready.\n";
    //t.join();
    return 0;
}
// ConsoleApplication24.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>

template<typename T>
void foo(const T& val)
{

    if(std::is_pointer<T>::value)
    {

        std::cout << "pointer";
    }
    else
    {
        std::cout << "value";
    }

}
template <typename T>
void foo_impl(const  T& val, std::true_type)
{
    std::cout << *val << std::endl;
}

template <typename T>
void foo_impl(const T& val, std::false_type)
{
    std::cout << val << std::endl;
}

template <typename T>
void foo_(const T& val)
{
    foo_impl(val, std::is_pointer<T>());
}
template <>
void foo_impl(const int& val, std::false_type)
{
    std::cout << "not int" << std::endl;
}

template <>
void foo_impl(const  int& val, std::true_type)
{
    std::cout <<val << std::endl;
}
template <typename T>
void foo1_(const T& val)
{
    foo_impl(val, std::is_integral<T>());
}
//
//template<typename T1,typename T2>
//struct commmon_type<T1,T2>
//{
//    typedef decltype(true ? std::declval<T1>() : std::declval<T2>()) type;
//};
template<typename T1,typename T2>
typename std::common_type<T1, T2>::type min(const T1& x, const T2& y);

int main()
{
    foo<int>(2);
    std::cout << "\n";
    const char* p = "hello";
    foo<const char*>(p);
    std::cout << "\n";

    foo_<const char*>(p);
    std::cout << "\n";
    foo1_<int>(2);
    std::cout << "Hello World!\n";
    foo1_(2.4);
   // commmon_type<std::string,const char*> f;
    long f = 9;
    //min(2, f);
   // min<int, long>(2, 8);
    const char* s = "hello";
    std::string str=s;
    //const char* ss = str;
    std::cout << str;
}
// ConsoleApplication4.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iterator>
#include<algorithm>
#include <functional>
#include<string>
#include <iostream>
#include <vector>
using namespace std;
template<typename T>
inline void PRINT_ELMENT(const T& coll)
{
    for(const auto elem:coll)
    {
        cout << elem<<"\n";
    }
}
void fun(int a)
{
    cout << a;

}

int main()
{
    vector<string> cotainer;

    ////input still you are  enter ctrl+c
    //copy(istream_iterator<string>(cin), istream_iterator<string>(), back_inserter(cotainer));
    //sort(cotainer.begin(), cotainer.end());
    //unique_copy(cotainer.begin(), cotainer.end(), ostream_iterator<string>(cout, ","));
    int i = 90;
    bind(fun, 1)();
    bind(fun, ref(i))();
    vector<int> f{ 1,2,3,4,5 };

    iter_swap(f.begin(), prev(f.end()));
    for(auto i:f)
    {
        cout << i;
    }

}


#include <iostream>                // std::cout

#include <thread>                // std::thread

#include <mutex>                // std::mutex, std::unique_lock

#include <condition_variable>    // std::condition_variable



std::mutex mtx; // 全局互斥锁.

std::condition_variable cv; // 全局条件变量.

bool ready = false; // 全局标志位.



void do_print_id(int id)

{

    std::unique_lock <std::mutex> lck(mtx);

    while (!ready) // 如果标志位不为 true, 则等待...

        cv.wait(lck); // 当前线程被阻塞, 当全局标志位变为 true 之后,

    // 线程被唤醒, 继续往下执行打印线程编号id.

    std::cout << "thread " << id << '\n';

}



void go()

{

    std::unique_lock <std::mutex> lck(mtx);

    ready = true; // 设置全局标志位为 true.

    cv.notify_all(); // 唤醒所有线程.

}



int main()

{

    std::thread threads[10];

    // spawn 10 threads:

    for (int i = 0; i < 10; ++i)

        threads[i] = std::thread(do_print_id, i);



    std::cout << "10 threads ready to race...\n";

    go(); // go!



    for (auto& th : threads)

        th.join();



    return 0;

}

// ConsoleApplication25.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <future>
#include <iostream>
#include <random>
#include <thread>
using namespace std;
int dosomething(char c)
{
    default_random_engine dre(c);
    uniform_int_distribution<int> id(10, 1000);
    for(int i=0;i<10;++i)
    {
        this_thread::sleep_for(chrono::milliseconds(id(dre)));
        cout.put(c).flush();
    }
    return c;
}
int fun1()
{
    return dosomething('-');
}
int fun2()
{
    return dosomething('+');
}
int main()
{
    std::cout << "Hello World!\n";
    std::future<int> result1(std::async(fun1));
    int result2 = fun2();
    int result = result1.get() + result2;
   // cout <<( result1 + result2);
    cout << result;
}



This content originally appeared on DEV Community and was authored by 海前 王


Print Share Comment Cite Upload Translate Updates
APA

海前 王 | Sciencx (2024-08-27T02:31:35+00:00) c++11. Retrieved from https://www.scien.cx/2024/08/27/c11/

MLA
" » c++11." 海前 王 | Sciencx - Tuesday August 27, 2024, https://www.scien.cx/2024/08/27/c11/
HARVARD
海前 王 | Sciencx Tuesday August 27, 2024 » c++11., viewed ,<https://www.scien.cx/2024/08/27/c11/>
VANCOUVER
海前 王 | Sciencx - » c++11. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/27/c11/
CHICAGO
" » c++11." 海前 王 | Sciencx - Accessed . https://www.scien.cx/2024/08/27/c11/
IEEE
" » c++11." 海前 王 | Sciencx [Online]. Available: https://www.scien.cx/2024/08/27/c11/. [Accessed: ]
rf:citation
» c++11 | 海前 王 | Sciencx | https://www.scien.cx/2024/08/27/c11/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.