2013-03-18 83 views
-1

string = std::vector<int>(6,0),並希望爲{ 0 0 0 0 0 0 }字符串解析爲一個特定的格式

我想這

#include <iostream> 
using namespace std; 


int main() 
{ 
    string str = "std::vector<int>(6,0)" ; 

    unsigned found = str.find('('); 
    char c = str[found+1]; 
    int i = c - '0'; 
    char ch = str[found+3]; 
    int j = ch - '0'; 

    str = "{ "; 
    for(int k = 0; k < i ; k++) 
    { 
     str = str + ch + " " ; 
    } 

    str = str + " }"; 

    cout << str << endl; 

    return 0; 
} 

它的工作原理,但並沒有看起來非常高效的IT顯示。有什麼更好的想法

+0

這可能是矯枉過正,但我​​敢肯定,[漂亮的打印](http://stackoverflow.com/questions/4850473/pretty-print-c-stl-containers)可以處理它。 – chris 2013-03-18 15:15:46

+0

效率高但不靈活。 – deepmax 2013-03-18 15:15:52

+0

如果您正在尋找更靈活的方式來解析這些內容,您可能需要使用std :: regex或boost :: spirit。正則表達式永不放棄 – 2013-03-18 15:17:22

回答

0

當前版本的代碼僅適用於矢量和元素大小的單個數字值。使用std::stringstream了一下更好的解決方案:

#include <iostream> 
#include <sstream> 

using namespace std; 


int main() 
{ 

    string str = "std::vector<int>(6,0)" ; 

    unsigned found = str.find('('); 
    string arguments = str.substr(found); 

    // Replace non-numeric chars with spaces. 
    for (unsigned i=0;i<arguments.size(); ++i) { 
    if (!isdigit(arguments[i])) { 
     arguments[i] = ' '; 
    } 
    } 

    std::istringstream iss(arguments); 
    int size; 
    // String to avoid having to cast later on 
    string value; 
    iss >> size >> value; 

    string res = "{; 

    for (int i = 0; i < size; ++i) { 
    res += " " + value; 
    } 
    res += " }"; 

    cout << res; 
    return 0; 
} 
+0

大小和值都沒有任何價值:-( – dharag 2013-03-18 16:09:24

+0

我很愚蠢對不起!!將參數作爲參數傳遞給iss-s構造函數。我修復了這個問題。對不起。 – 2013-03-18 16:11:11

+0

This Works!Thank you – dharag 2013-03-18 16:53:42

0

下面是更靈活(希望)位的代碼的另一個版本。它發現「(」sing,then「)」用逗號分割它們,去掉所有的空白字符並將數字轉換爲整數。然後它將它們打印出來。

#include <string> 
#include <iostream> 
using namespace std; 

//these three functions are taken from here: 
//http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring 

#include <algorithm> 
#include <functional> 
#include <cctype> 
#include <locale> 

static inline std::string &ltrim(std::string &s) { 
     s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace)))); 
     return s; 
} 

// trim from end 
static inline std::string &rtrim(std::string &s) { 
     s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end()); 
     return s; 
} 

// trim from both ends 
static inline std::string &trim(std::string &s) { 
     return ltrim(rtrim(s)); 
} 

int main() 
{ 
    string s = "std::vector<int>(612,30)"; 

    int paren_start = s.find("(")+1; 

    string numbers = s.substr(paren_start, s.find(")")-paren_start); 
    int comma_pos = numbers.find(","); 

    string first_num = numbers.substr(0, comma_pos); 
    string second_num = numbers.substr(comma_pos+1, numbers.size()-comma_pos); 

    int first = atoi(trim(first_num).c_str()); 
    int second = atoi(trim(second_num).c_str()); 

    cout << "{" << endl; 
    for(int i=0; i<first; i++) 
    { 
     cout << second << " "; 
    } 
    cout << "}" << endl; 

    return 0; 
} 
+0

此解決方案在大多數情況下運行良好的情況除外,如果第二個數字如果浮動像std :: vector (6,0.0)我試着改變atoi到atof和'第二個'變量浮動,但沒有工作任何想法如何使它工作感謝 – dharag 2013-03-27 13:36:14

+0

也許你忘了在「second」聲明中將int更改爲double? – d33tah 2013-03-27 19:54:29

+0

我知道了..謝謝! – dharag 2013-03-28 14:43:27