[問題] 一段語法詢問

作者: gecer (gecer)   2018-03-24 08:53:15
http://www.cplusplus.com/reference/mutex/mutex/lock/
// mutex::lock/unlock
#include <iostream> // std::cout
#include <thread> // std::thread
#include <mutex> // std::mutex
std::mutex mtx; // mutex for critical section
void print_thread_id (int id) {
// critical section (exclusive access to std::cout signaled by locking mtx):
mtx.lock();
std::cout << "thread #" << id << '\n';
mtx.unlock();
}
int main ()
{
std::thread threads[10];
// spawn 10 threads:
for (int i=0; i<10; ++i)
threads[i] = std::thread(print_thread_id,i+1);
for (auto& th : threads) th.join();
return 0;
}
請問黃字 auto 的意思? 是不是等於foreach loop?
作者: stupid0319 (徵女友)   2018-03-24 09:05:00
自動指定類型?
作者: steve1012 (steve)   2018-03-24 09:16:00
讓編譯器推導type 跟你想說的一樣
作者: jerryh001   2018-03-24 09:38:00
"auto"是自動推定 那一整段的寫法是forloop*foreach loop

Links booklink

Contact Us: admin [ a t ] ucptt.com