0

string = std::vector<int>(6,0) and want it to be displayed as { 0 0 0 0 0 0 }

I tried this

#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 works but does not looks very efficient. Any better idea ?

Andbdrew
  • 11,788
  • 4
  • 33
  • 37
dharag
  • 299
  • 1
  • 8
  • 17
  • It could be overkill, but I'm sure the [pretty printer](http://stackoverflow.com/questions/4850473/pretty-print-c-stl-containers) can handle it. – chris Mar 18 '13 at 15:15
  • It's efficient but inflexible. – masoud Mar 18 '13 at 15:15
  • You might want to use std::regex or boost::spirit if you're looking for a more flexible way to parse such content. Regex never lets down – Ed Rowlett-Barbu Mar 18 '13 at 15:17
  • So you're parsing a string that happens to contain C++ code and want to print what you think it contains? Is this for a code analysis tool? If not, perhaps there's a better way to do it. – metal Mar 18 '13 at 15:19
  • This will not work if the size of the vector is two-digit number – Ivaylo Strandjev Mar 18 '13 at 15:20
  • Thanks yeah this is very inflexible. The values might change and the code breaks here. – dharag Mar 18 '13 at 15:21

2 Answers2

0

The current version of the code will only work for single digit values for both the size of the vector and the elements. Use std::stringstream for a bit better solution:

#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;
}
Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
0

Here's another version of the code that is (hopefully) a bit more flexible. It finds the "(" sing, then ")", splits them with a comma, strips all the whitespace characters and converts the numbers into integers. Then it prints them out.

#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;
}
d33tah
  • 10,999
  • 13
  • 68
  • 158
  • This solution worked well in most of the situations except when the second number if float like std::vector(6,0.0) I tried changing atoi to atof and 'second' variable to float but doesnt work. Any idea how to make it work thanks – dharag Mar 27 '13 at 13:36
  • Perhaps you forgot to change int to double in "second" declaration? – d33tah Mar 27 '13 at 19:54