parseToNumbers by std::regex
Hello!
My code must write numbers on the screen, but it dodn't do it
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
#include <vector>
#include <string>
#include <iostream>
#include <regex>
void parseToNumbers( const std::vector<std::string> strs, std::vector<double> nums );
void showNums( const std::vector<double> &nums );
int main()
{
// Fill
std::vector<std::string> strs;
strs.push_back( "0,0.693147181" );
strs.push_back( "0.01,0.688135141" );
strs.push_back( "0.02,0.683100885" );
strs.push_back( "0.03,0.678047248" );
strs.push_back( "0.04,0.672977126" );
strs.push_back( "0.05,0.667893473" );
// Parse
std::vector<double> nums;
parseToNumbers( strs, nums);
// Show
showNums( nums );
return 0;
}
void parseToNumbers( const std::vector<std::string> strs, std::vector<double> nums )
{
try {
const std::regex re( "\\d+" );
std::smatch sm;
for( size_t i = 0; i < strs.size( ); ++i ) {
std::regex_match( strs[i], sm, re);
// if ( sm.size() != 0 ) {
std::cout << sm[0] << std::endl;
// }
}
} catch ( const std::regex_error &e ) {
std::cerr << "Error: incorrect regular expression" << std::endl;
return;
}
}
void showNums( const std::vector<double> &nums )
{
for( size_t i = 0; i < nums.size(); ++i ) {
std::cout << nums[i] << std::endl;
}
}
|
Thank you!
Last edited on
You're passing "nums" by value to parseToNumbers()
Thank you! And sorry for my example above. It doesn't work:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
#include <string>
#include <iostream>
#include <regex>
int main()
{
const std::string str = "0,0.693147181";
try {
const std::regex re( "\\d+" );
std::smatch sm;
std::regex_match( str, sm, re);
std::cout << sm[0] << std::endl;
} catch ( const std::regex_error &e ) {
std::cerr << "Error: incorrect regular expression" << std::endl;
return 1;
}
return 0;
}
|
Topic archived. No new replies allowed.