2013-03-18 2 views
-1

string = std::vector<int>(6,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; 
} 

을 시도

{ 0 0 0 0 0 0 }로 표시하려는. 더 좋은 생각이야?

+0

그것은 잔인한 사람이 될 수 있지만, 나는 [꽤 프린터 (http://stackoverflow.com/questions/4850473/pretty-print-c-stl-containers)를 처리 할 수 ​​있습니다 확신합니다. – chris

+0

효율적이지만 유연하지 않습니다. – deepmax

+0

당신은 std :: regex 또는 boost :: spirit을 사용하여 이러한 컨텐트를 더 유연하게 분석 할 수 있습니다. Regex는 결코 –

답변

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

+0

나는 그것을 바꿔서 미안 해요! 인수를 인수로 인수 생성자에 전달합니다. 죄송합니다 . –

+0

이 작동합니다! 감사합니다. – dharag

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 :: float처럼 float이 될 때를 제외하고 상황을 말합니다. atoi를 atof로 변경하고 '두 번째'변수를 부동 상태로 변경하려고 시도했지만 아무런 아이디어가 없습니다. 감사합니다. – dharag

+0

아마도 잊어 버렸을 것입니다. "두 번째"선언에서 int를 두 배로 변경 하시겠습니까? – d33tah

+0

나는 그것을 얻었습니다. 두려움! – dharag

관련 문제