2012-03-20 6 views
0

関数の変数を主なスコープに渡したいと思います。私はCで行うのと同じようにしようとしていますが、何も返しません。関数から "main"スコープに変数(配列型)を渡します。型:std :: tr1 :: match_results <std :: string :: const_iterator>

私はコンパイルして実行すると、出力が出力できると関数の戻り

#include "StdAfx.h" 
#include <regex> 
#include <iostream> 
#include <string> 
#include <conio.h> 
using namespace std; 

std::tr1::match_results<std::string::const_iterator> match(std::string& regex, const std::string& ip,std::tr1::match_results<std::string::const_iterator> res) 
{ 
    const std::tr1::regex pattern(regex.c_str()); 
    bool valid = std::tr1::regex_match(ip, res, pattern); 
    std::cout << ip << " \t: " << (valid ? "valid" : "invalid") << std::endl; 
    cout << "FIRST RES FOUND: " << res[1] << endl; 
    return res; 
} 

int main() 
{ 

    string regex = "(\\d{1,3}):(\\d{1,3}):(\\d{1,3}):(\\d{1,3})"; 
    string ip = "49:22:33:444"; 
    std::tr1::match_results<std::string::const_iterator> res; 
    match(regex,ip.c_str(), res); 

    cout << "Result >" << res[1] << "< " << endl; 


    _getch(); return 0; 
} 

後にそれに対処したい:「最初に見つかったRES:49 結果> <」

それはおそらく、本当にシンプルなソリューションですが、何を、私は私のメインは、のようにそれを正しく読むことができるためにそれを設定しなければならない。事前に「結果> 49 <」

感謝を。 :)

答えて

3

オプション1:

void match(string& regex, const string& ip, tr1::match_results<string::const_iterator> & res) 
{ 
    const tr1::regex pattern(regex.c_str()); 
    bool valid = tr1::regex_match(ip, res, pattern); 
    cout << ip << " \t: " << (valid ? "valid" : "invalid") << endl; 
    cout << "FIRST RES FOUND: " << res[1] << endl; 
} 

オプション2:参照使用した値で結果を返して、それを格納します。

tr1::match_results<string::const_iterator> match(string& regex, const string& ip) 
{ 
    tr1::match_results<string::const_iterator> res; 
    // ... 
    return res; 
} 

int main() 
{ 
    // ... 
    tr1::match_results<string::const_iterator> res = match(regex, ip); 
} 

を別のノートでは、のために絶対に必要があってはなりませんすべてのc_str()コールは、<regex>が完全に機能するstd::stringインターフェイスを持っています。詳細についてはドキュメントをチェックしてください。ちょうど2つのタイプ名が必要です。


編集:ここではstd::stringを使用して上のいくつかの基本的な例です。 std::wstringchar*およびwchar_t*の同等の構成がありますが、std::stringが最も有用なものです。

<regex>のサポートにはまだパッチがついていますので、TR1とBoostの代替案も考慮する必要があります。私はすべての3つを提供し、あなたは1を選ぶことができます。

namespace ns = std;   // for <regex> 
namespace ns = std::tr1;  // for <tr1/regex> 
namespace ns = boost;  // for <boost/regex.hpp> 

ns::regex r(""); 
ns::smatch rxres;   // 's' for 'string' 

std::string data = argv[1]; // the data to be matched 

// Fun #1: Search once 

if (!ns::regex_search(data, rxres, r)) 
{ 
    std::cout << "No match." << std::endl; 
    return 0; 
} 


// Fun #2: Iterate over all matches 

ns::sregex_iterator rt(data.begin(), data.end(), r), rend; 

for (; rt != rend; ++rt) 
{ 
    // *rt is the entire match object 

    for (auto it = rt->begin(), end = rt->end(); it != end; ++it) 
    { 
     // *it is the current capture group; the first one is the entire match 

     std::cout << " Match[" << std::distance(rt->begin(), it) << "]: " << *it << ", length " << it->length() << std::endl; 
    } 
} 

はタイプns::regex_errorの例外を処理することを忘れないでください。

0

resを値ではなく参照で渡します。換言すれば、を値の代わりに参照として宣言してください。type &resではなく、type resです。

関連する問題