zoukankan      html  css  js  c++  java
  • c++ regex 正则类例子及其gcc4.8报错

    参考:c++ regex类


    例子

    #include <iostream>
    #include <string>
    #include <regex>
    using namespace std;
    int main(void) 
    {
    	cout << "hello world" <<endl;
    	std::string str("zh_CN");
    	std::string pref ("zh-CN");
    	cout << "str: "<< str <<endl;
    	cout << "perf: " <<pref <<endl;
    
    	cout << "compare: "<<str.compare("zh")<<endl;
    	
    	size_t sep = str.find ('_');
    	std::regex locale(str.substr(0,sep)+ "-[A-Za-z]*"  +str.substr(sep+1));	 //new 一个正在
    	
    	cout << "create locale ok"<<endl;
    	cout << "is same: "<< std::regex_match("zh-CN",locale) << endl;
    	return 0;
    }
    

    报错记录
    1.使用g++ 编译 g++ -o regex regex.cpp

     error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options
    

    解决:是由于新版c++11 需要 -std=c++11

    2.使用 g++ -o regex regex.cpp -std=c++11
    ./regex 后出现段错误

    terminate called after throwing an instance of 'std::regex_error'
      what():  regex_error
    Aborted (core dumped)
    

    查网上原因是因为gcc 4.8 对新特性c++11的regex不完全支持
    见参考Is gcc 4.8 or earlier buggy about regular expressions?

    我的g++ 版本

    g++ --version
    g++ (Ubuntu 4.8.4-2ubuntu1~14.04.4) 4.8.4
    Copyright (C) 2013 Free Software Foundation, Inc.

    更换g++ 4.9试试 就可以了

    /usr/bin/g++-4.9 -o regex regex.cpp -std=c++11
    ~/test_2020.01.07/c++_func$ ./regex 
    hello world
    str: zh_CN
    perf: zh-CN
    compare: 3
    create locale ok
    is same: 1
    
  • 相关阅读:
    第二次作业循环语句
    c语言01次作业分支,顺序结构
    PAT 1027. Colors in Mars
    PAT 1026 Table Tennis
    PAT 1035 Password
    PAT 1038. Recover the Smallest Number
    PAT 1028 List Sorting (25)
    PAT 1041 Be Unique (20)
    PAT 1025 PAT Ranking
    1037. Magic Coupon
  • 原文地址:https://www.cnblogs.com/rootshaw/p/12910684.html
Copyright © 2011-2022 走看看