Разбить строку на список в C++ | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
0

Разбить строку на список в C++

Здравствуйте, у меня такой вопрос... У меня есть код: #include <iostream> using namespace std; int main() { string str1; cout << "Enter: "; cin >> str1; } Суть вопроса в том, как сделать, что бы при вводе строка развивалась на список? Например я введу " one two three ", а оно должно развиваться на список( массив ) то есть , что бы потом можно было манипулировать им ... one будет иметь индекс 0, two - 1, three - 2?!

28th Feb 2021, 6:01 PM
Bogdan Bogdanov
Bogdan Bogdanov - avatar
2 ответов
+ 1
int main() { char str[] = "one two three"; char *token = strtok(str, " "); // Keep printing tokens while one of the // delimiters present in str[]. while (token != NULL) { printf("%s\n", token); token = strtok(NULL, " "); } return 0; }
28th Feb 2021, 7:54 PM
N A I E B I
N A I E B I - avatar
0
Translations: Split a string into a list in C++ Hello, I have such a question... I have a code: #include <iostream> using the std namespace; int main() { string str1; stream cout < < " enter: "; Kin > > str1 looks like this; } The essence of the question is, how do I make the string develop into a list when I type? For example, I will enter "one two three", and it should develop into a list (array) that is, so that you can then manipulate it ... one-0, two-1, three-2?!
28th Feb 2021, 6:04 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar