Pythonにおいて"Abcde"[3:] -> "cd"
と書けます。これをC++で簡潔に書くにはどうしたらいいですか?
1 件の回答
std::string_view::substr()
が使えます。こんな感じでどうでしょうか?
#include <iostream>
#include <string_view>
using namespace std::literals;
int main() {
auto const before = "Abcde"sv;
auto const after = before.substr(3);
std::cout << "before: "sv << before << std::endl;
std::cout << "after: "sv << after << std::endl;
}
"Abcde"[3:4]
の結果は"de"
にはならないと思うのですが。それからC++側に何か条件が付いているのでは? 何も条件が無いのでは想定範囲が広すぎると思われます。