简体   繁体   English

C ++中类似python的字符串索引

[英]python-like string indexing in C++

Is there any (possibly, using a macros) way to get a substring of a string using python-like expression f(i:j)? 是否有任何(可能使用宏)方法使用类似python的表达式f(i:j)获取字符串的子串? Or, more specifically, resolve i:j expression into pair of indices i and j? 或者,更具体地说,将i:j表达式解析为索引i和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 ij", I guess. 所以问题更多的是“我能把i:j变成ij”,我猜。 Doesn't matter is these are std:strings or c-strings. 无关紧要的是std:strings或c-strings。

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. 如果我发现你使用这种技术,我永远不会雇用你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM