Jump to content

  • Log In with Google      Sign In   
  • Create Account


Khatharr

Member Since 24 Apr 2010
Online Last Active Today, 12:00 AM
*****

Topics I've Started

More fun with std::regex

28 May 2014 - 09:11 AM

In Ruby I can say:

"one two buckle my shoe ".gsub(/(.*?) /) {|m| p m}

And get the expected result (each word is printed to its own line).

 

I'm trying to do the same in C++, but it's being dippy:

#include <string>
#include <regex>
#include <iostream>

using namespace std;

int main() {
  string sentence("one two buckle my shoe ");
  regex exp("(.*?) ");
  smatch words;
  regex_search(sentence, words, exp);
  for(auto word : words) {cout << "\"" << word << "\"" << endl;}
  system("pause");
  return 0;
}

It produces:

"one " //why is it not matching the whole string?
"one"

Whereas I want it to produce:

"one two buckle my shoe " //I'll discard this annoying false match one the problem is solved
"one"
"two"
"buckle"
"my"
"shoe"

Can anyone see what I'm doing wrong here?


[C++] LSP and Rule of Three

24 September 2013 - 05:10 PM

An interesting question occurred to me today.

 

In order to observe LSP, if a base class has an explicit copy ctor / assignment operator, do the derived classes all need explicit versions between themselves and the other classes? In other words, if parent Foo has an explicit copy ctor, does Bar need conversion ctors from Foo and Baz? Does Foo need conversion assignment operators for each of its derived classes?

 

I'm thinking:

Foo* a = new Foo;
Foo* b = new Bar;
Foo* c = new Baz;
*a = *b; //what happens?

Does C++ <random> lib require seeding?

19 September 2013 - 10:06 PM

I've implemented a function:

bool doesCustomerArrive(int custPerHour) {
  if(custPerHour > 60) {return true;}
  static default_random_engine generator;
  float custPerMinute = (float)custPerHour / 60;
  bernoulli_distribution distribution(custPerMinute);
  return distribution(generator);
}

Which I'm using in a simulation. I iterate custPerHour, starting from 1, and run the thing 6000 times per iteration, doing some calculations until a specific result is met. My problem is that the program is producing the exact same unusual results every time it's run, which immediately makes me think that I've either failed to seed the randomizer or else just set this up wrong. I thought this new set of random functions didn't need seeding, and I don't see anything wrong with my algorithm. Where am I screwing up here?

 

Specifically, the problem results are:

39 CPH for 100 hours = 2016 customers (expected 3900)

40 CPH for 100 hours = 1986 customers (expected 4000)

 

The results are the same every time.


Placement new

08 September 2013 - 10:08 PM

Obviously I'm missing something here, so I'll post in the beginner's forum.

 

Query:

 

Why would I ever use placement new?


istream.operator>> == whyDidYouDoThis

02 September 2013 - 05:44 PM

So why is it that the majority of the time I want cin to behave a certain way, and I think a lot of other people probably want it to behave that way as well. The thing that's bothering me is that, not only is it implemented differently, but there's not even stream manipulators to make it behave the way people want it to.

 

What am I talking about?

 

Well...

 

I wrote this:

template <typename T>
T getUserValue(const string& requestString) {
  T retval;
  while(true) {
    cout << requestString;
    cin >> retval;

    bool ok = cin.good();
    cin.clear();
    cin.ignore(numeric_limits<streamsize>::max(), '\n'); //whyyyyyy

    if(ok) {return retval;}
    cout << "Invalid input." << endl;
  }
}

Ignoring the annoyance of cin.ignore to discard the newline character, this works just fine for grabbing ints and floats. It works just fine for grabbing a std::string, UNLESS the string has a space in it - and there's my main problem.

 

So what I want to know is...

 

Why aren't there manipulators that solve these common problems? Why can't I just say:

template <typename T>
T getUserValue(const string& requestString) {
  T retval;
  while(true) {
    cout << requestString;
    cin >> allow_spaces >> consume_eol >> retval;
    if(cin.good()) {return retval;}
    cin.clear();
    cout << "Invalid input." << endl;
  }
}

Now I'm not asking you to tell me about std::getline(). I understand that I can get lines that way. I'm just wondering why cin is so clumsy, having all these pitfalls in the face of common, expected usage, and doesn't have the obvious fixes available to it.

 

Am I missing something here?


PARTNERS