Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python-like string indexing in C++

Tags:

c++

python

string

Is there any (possibly, using a macros) way to get a substring of a string using python-like expression f(i:j)? Or, more specifically, resolve i:j expression into pair of indices i and j? Any ideas?

EDIT: Yes, I need :. Or ;. Basically, something that simple function or macros can't do. Just want to see if it is possible or not.

EDIT: Basically, I want to see if it is widely applicable. For arrays as well, maybe. So the question is more of a "Can I turn i:j to i j", I guess. Doesn't matter is these are std:strings or c-strings.

like image 723
maksay Avatar asked Dec 01 '22 03:12

maksay


1 Answers

I hate myself for answering, but ...

#include <iostream>
#include <string>

#define f(x) substr(true?x, false?x)

int main () {
  std::string s = "Hello, world";
  std::string y = s.f(1:4);
  std::cout << y << "\n";
}

Warning: I am a hiring manager. If I ever discover that you use this technique, I will never hire you.

like image 164
Robᵩ Avatar answered Dec 21 '22 04:12

Robᵩ