c++11 学习及测试(emplace_back实现方式 数学分布 正则表达式)
2016-03-31 10:42
477 查看
可变长度参数列表,参数原始类型转发(emplace_back实现方式)
#include <iostream>
#include <utility>
#include <string>
#include <vector>
using namespace std;
template<typename T, typename ... Args>
T make(T, Args&& ... rest) { //模版参数列表,右值引用调用构造函数
cout << sizeof...(rest) << endl;
return T(std::forward<Args>(rest) ... ); //按原始类型转发扩展参数,emplace函数的事项方式
}
int main(int argc, char* argv[]) {
string x;
string s = make(x, 10, 'c');
cout << s << endl;
vector<string> v;
v.emplace_back(10, 'c');
cout << v[0] << endl;
cout << "Hello world!" << endl;
return 0;
}按分布采样
#include <iostream>
#include <regex>
using namespace std;
int main(int argc, char* argv[]) {
regex r("[[:alnum:]]+\\.(cpp|cxx|cc)$", regex::icase);
smatch results;
string file;
// regex_search("a.cc", results, r); // 编译错误,char* 对应cmatch, string 对应smatch
cmatch res;
regex_search("a.cc", res, r);
while(cin >> file) {
if(regex_search(file, results, r)) {
cout << results.str() << endl;
} else {
cout << "error file name" << endl;
}
}
return 0;
}正则表达式子匹配,迭代器
#include <iostream>
#include <regex>
using namespace std;
int main(int argc, char* argv[]) {
string pattern("([^c]ei)");
pattern = "[[:alpha:]]*" + pattern + "[[:alpha:]]*";
string file = "you are my freiNd, theif";
regex r(pattern, regex::icase);
for(sregex_iterator it(file.begin(), file.end(), r), end_it; it != end_it; ++it) {
cout << it->str() << endl;
cout << it->size() << endl;
cout << it->prefix() << endl;
cout << it->suffix() << endl;
cout << it->length(1) << endl;
cout << it->str(1) << endl;
if((*it)[0].matched) {
for(auto jt = (*it)[0].first; jt != (*it)[0].second; ++jt) {
cout << "[" << *jt << "]" << endl;
}
}
}
string phone = "(\\()?(\\d{3})(\\))?([-. ])?(\\d{3})([-. ])?(\\d{4})";
regex p(phone);
string fmt = "$2.$5.$7";
string s = "";
s = s + "morgan (201) 555-2365 123-345-1234\n" +
"drew (123)123.1234\n" +
"lee (789) 999-0987 1234567890 900.000.0000";
cout << regex_replace(s, p, fmt) << endl;
cout << s << endl;
return 0;
}
#include <iostream>
#include <utility>
#include <string>
#include <vector>
using namespace std;
template<typename T, typename ... Args>
T make(T, Args&& ... rest) { //模版参数列表,右值引用调用构造函数
cout << sizeof...(rest) << endl;
return T(std::forward<Args>(rest) ... ); //按原始类型转发扩展参数,emplace函数的事项方式
}
int main(int argc, char* argv[]) {
string x;
string s = make(x, 10, 'c');
cout << s << endl;
vector<string> v;
v.emplace_back(10, 'c');
cout << v[0] << endl;
cout << "Hello world!" << endl;
return 0;
}按分布采样
#include <iostream> #include <random> using namespace std; int main(int argc, char* argv[]) { default_random_engine e(time(0)); for(int i =0; i < 10; ++i) { cout << e() << endl; } uniform_real_distribution<> u(0, 1); for(int i = 0; i < 10; ++i) { cout << u(e) << endl; } normal_distribution<> n(4, 1.5); //均值 标准差 for(int i = 0; i < 10; ++i) { cout << n(e) << endl; } cout << "Hello world!" << endl; return 0; }正则表达式类
#include <iostream>
#include <regex>
using namespace std;
int main(int argc, char* argv[]) {
regex r("[[:alnum:]]+\\.(cpp|cxx|cc)$", regex::icase);
smatch results;
string file;
// regex_search("a.cc", results, r); // 编译错误,char* 对应cmatch, string 对应smatch
cmatch res;
regex_search("a.cc", res, r);
while(cin >> file) {
if(regex_search(file, results, r)) {
cout << results.str() << endl;
} else {
cout << "error file name" << endl;
}
}
return 0;
}正则表达式子匹配,迭代器
#include <iostream>
#include <regex>
using namespace std;
int main(int argc, char* argv[]) {
string pattern("([^c]ei)");
pattern = "[[:alpha:]]*" + pattern + "[[:alpha:]]*";
string file = "you are my freiNd, theif";
regex r(pattern, regex::icase);
for(sregex_iterator it(file.begin(), file.end(), r), end_it; it != end_it; ++it) {
cout << it->str() << endl;
cout << it->size() << endl;
cout << it->prefix() << endl;
cout << it->suffix() << endl;
cout << it->length(1) << endl;
cout << it->str(1) << endl;
if((*it)[0].matched) {
for(auto jt = (*it)[0].first; jt != (*it)[0].second; ++jt) {
cout << "[" << *jt << "]" << endl;
}
}
}
string phone = "(\\()?(\\d{3})(\\))?([-. ])?(\\d{3})([-. ])?(\\d{4})";
regex p(phone);
string fmt = "$2.$5.$7";
string s = "";
s = s + "morgan (201) 555-2365 123-345-1234\n" +
"drew (123)123.1234\n" +
"lee (789) 999-0987 1234567890 900.000.0000";
cout << regex_replace(s, p, fmt) << endl;
cout << s << endl;
return 0;
}
相关文章推荐
- C语言字符串查找位置
- 28.C语言内存管理机制
- c++ string 中的十六进制数字转十六进制字符
- C++多个文件共享同一头文件时全局变量的声明
- c++封装链表实现-->学生信息管理分析系统
- C++在游戏中开发中发送多于1K的数据
- C++ pair(对组)用法
- c++数据类型万能转换器boost::lexical_cast
- CUDA CUBIN/PTX文件动态加载
- 一种实现C++反射功能的想法(二)
- linenoise 源码分析(一)
- 用C++实现高精度加法运算
- C语言字符型数据(一)—简单的恺撒密码
- 【poj 1942】Paths on a Grid 题意&题解&代码(C++)
- C语言小结(一)
- UE4 C++ Tips整理
- C语言,以空格为分割,以回车为结束标记,输入一串数字到一个int数组中
- C++中 字符替换注意点,适合新手 - char
- void与void*详解
- 【poj 3252】Round Numbers 题意&题解&代码(C++)