Created
March 3, 2012 02:23
-
-
Save JosephLaurino/1963816 to your computer and use it in GitHub Desktop.
C++11 with Visual Studio 11 Beta
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// testing out Visual Studio 11 Beta's support of | |
// the latest C++11 standard | |
// | |
// code snippets from | |
// http://en.wikipedia.org/wiki/C%2B%2B11 | |
// http://solarianprogrammer.com | |
// | |
#include "stdafx.h" | |
#include <iostream> | |
#include <thread> | |
#include <regex> | |
using namespace std; | |
static const int num_threads = 10; | |
//This function will be called from a thread | |
void call_from_thread(int tid) { | |
cout << endl << "Launched by thread " << tid; | |
} | |
int test_threads() | |
{ | |
cout << endl << "test_threads" << endl; | |
thread t[num_threads]; | |
//Launch a group of threads | |
for (int i = 0; i < num_threads; ++i) { | |
t[i] = thread(call_from_thread, i); | |
} | |
cout << endl << "Launched from the main\n"; | |
//Join the threads with the main thread | |
for (int i = 0; i < num_threads; ++i) { | |
t[i].join(); | |
} | |
return 0; | |
} | |
void test_ranged_based_for_loop() | |
{ | |
cout << endl << "test_ranged_based_for_loop" << endl; | |
int my_array[5] = {1, 2, 3, 4, 5}; | |
for (int &x : my_array) { | |
x *= 2; | |
cout << x << endl; | |
} | |
} | |
void test_lambda() | |
{ | |
cout << endl << "test_lambda" << endl; | |
int n=10; | |
vector<int>v(n); | |
//initialize the vector with values from 10 to 1 | |
for(int i=n,j=0;i>0;i--,j++) | |
v[j]=i; | |
//print the unsorted vector | |
for(int i=0;i<n;i++) | |
cout<<v[i]<<" "; | |
cout<<endl; | |
//sort the vector using a lambda | |
sort(v.begin(),v.end(),[](int i,int j)->bool{return (i<j);}); | |
//print the sorted vector | |
for(int i=0;i<n;i++) | |
cout<<v[i]<<" "; | |
cout<<endl; | |
} | |
void test_regex() | |
{ | |
cout << endl << "test_regex" << endl; | |
string input; | |
regex integer("(\\+|-)?[[:digit:]]+"); | |
//As long as the input is correct ask for another number | |
while(true) | |
{ | |
cout<<"give me an integer [q to quit]: "<< endl; | |
cin>>input; | |
//Exit when the user inputs q | |
if(input=="q") | |
break; | |
if(regex_match(input,integer)) | |
cout<<"integer"<< endl; | |
else | |
{ | |
cout<<"Invalid input"<< endl; | |
} | |
} | |
} | |
int main() | |
{ | |
test_threads(); | |
test_ranged_based_for_loop(); | |
test_lambda(); | |
test_regex(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment