c++ - std::count over variable string counting variable string -
i'm trying write function parse xml returned me api, data returned variable, need ensure function work in cases.
here start of function error occurring (i believe)
using namespace std; vector<string> grabxmlvals(string xmlstring, string tofind){ vector<string> values; int amount = (int)count(xmlstring.begin(), xmlstring.end(), &tofind); (int = 0; < amount; i++)
i want count amount of times tofind string occurs in xmlstring know how many times loop needs run.
but when compile count statement :
error 1 error c2446: '==' : no conversion 'std::basic_string<char,std::char_traits<char>,std::allocator<char>> ' 'int' c:\program files\microsoft visual studio 12.0\vc\include\xutility 3078
not sure param not happy with, pretty sure not third because if give hardcoded constant still not happy, i've found lots of people using std::count
std::string::begin
, end online while searching answer, doing i'm not?
well, tofind
std::string
, &tofind
takes address. doing this:
std::string const* addr = &tofind; std::find (xmlstring.begin(), xmlstring.end(), addr);
so trying find pointer inside std::string
doesn't work.
what want this:
std::size_t count_substr(std::string const& str, std::string const& tofind) { std::size_t count = 0; std::size_t pos = 0; while(pos != std::string::npos) { pos = str.find (tofind, pos); if (pos != std::string::npos) { /* increment count */ count++; /* skip instance of string found */ pos++; } } return count; }
this not elegant thing, should need.
Comments
Post a Comment