S
S
Sky404Error2020-04-28 12:07:52
C++ / C#
Sky404Error, 2020-04-28 12:07:52

How to parse a string with choices in C++ Builder?

There is a line:
Привет {Друг|...|Мир}. Как {ты|...|твои дела}?

How can I get different variants of this line?
For example:
Привет Друг. Как ты?
OR
Привет Друг. Как твои дела?
OR
...
OR
Привет Мир. Как ты?
OR
Привет Мир. Как твои дела?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Zhilin, 2020-04-28
@Anton3

I propose the following general approach. Start up std::vector<std::vector<std::string>>. Each internal vector stores the choices for the next piece of the string. In particular, for pieces of a string between variable parts, we will assume that there is only 1 choice. Accordingly, the entire vector of vectors is a sequence of such variable parts. The task is divided into 2 parts:
1. Parsing the input string into our structure. We parse in a loop, pulling out 1 character from the source string, and accumulate into an intermediate state. We will need it std::stringfor the current variant and std::vector<std::string>for the current variable part.
2. The simplest thing is, when our vector of vectors is full, we create an empty std::stringone, and throw in 1 option from each internal vector.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question