Šioje temoje bus aptarta, kaip galime suskaidyti nurodytas eilutes į vieną žodį C++ programavimo kalba . Kai suskirstome žodžių grupę ar eilučių rinkinius į atskirus žodžius, tai vadinama padalintas arba eilutės padalijimas. Tačiau skaidyti eilutes galima tik naudojant kai kuriuos skyriklius, pvz., tarpą ( ), kablelį (,), brūkšnelį (-) ir kt., todėl žodžiai tampa individualūs. Be to, nėra iš anksto nustatytos padalijimo funkcijos, skirtos padalyti eilučių rinkinį į atskirą eilutę. Taigi, čia mes išmoksime skirtingus būdus, kaip padalyti eilutes į vieną C++.
Skirtingas būdas suskaidyti eilutes C++
- Norėdami padalinti eilutes, naudokite funkciją strtok().
- Norėdami padalinti eilutes, naudokite pasirinktinę split() funkciją
- Norėdami padalinti eilutę, naudokite funkciją std::getline().
- Norėdami padalinti eilutę, naudokite funkcijas find() ir substr().
Norėdami padalinti eilutes, naudokite funkciją strtok().
strtok(): Funkcija strtok() naudojama padalyti pradinę eilutę į dalis arba žetonus pagal perduotą skyriklį.
Sintaksė
char *ptr = strtok( str, delim)
Aukščiau pateiktoje sintaksėje strtok() turi du parametrus, the str , ir aš dalinuosi .
str : str yra originali eilutė, iš kurios funkcija strtok() suskaido eilutes.
java stygų palyginimas
aš dalinuosi : tai simbolis, naudojamas eilutei padalinti. Pavyzdžiui, kablelis (,), tarpas ( ), brūkšnelis (-) ir kt.
Grįžti : grąžina žymeklį, nurodantį kitus simbolius. Iš pradžių jis nurodo pirmąjį stygų žetoną.
Pastaba: funkcija strtok() modifikuoja pradinę eilutę ir kiekvieno funkcijos strtok() iškvietimo skyriklio pozicijoje įdeda NULL simbolį (' '). Tokiu būdu jis gali lengvai sekti žetono būseną.
Programa padalinti eilutes naudojant strtok() funkciją
Panagrinėkime pavyzdį, kaip padalyti eilutę C++ naudojant strtok() funkciją.
maiša duomenų struktūroje
Program.cpp
#include #include using namespace std; int main() { char str[100]; // declare the size of string cout << ' Enter a string: ' <<endl; cin.getline(str, 100); use getline() function to read a string from input stream char *ptr; declare ptr pointer ' , '); strtok() separate using comma (,) delimiter. cout << split function: endl; while loop check is not null (ptr !="NULL)" { print the token (null, } return 0; < pre> <p> <strong>Output</strong> </p> <pre> Enter a string: Learn how to split a string in C++ using the strtok() function. Split string using strtok() function: Learn how to split a string in C++ Using the strtok() function. </pre> <h3>Program to use custom split() function to split strings</h3> <p>Let's write a program to split sequences of strings in C++ using a custom split() function.</p> <p> <strong>Program2.cpp</strong> </p> <pre> #include #include #define max 8 // define the max string using namespace std; string strings[max]; // define max string // length of the string int len(string str) { int length = 0; for (int i = 0; str[i] != ' '; i++) { length++; } return length; } // create custom split() function void split (string str, char seperator) { int currIndex = 0, i = 0; int startIndex = 0, endIndex = 0; while (i <= len(str)) { if (str[i]="=" seperator || i="=" endindex="i;" string substr ; substr.append(str, startindex, - startindex); strings[currindex]="subStr;" currindex +="1;" startindex="endIndex" 1; } i++; int main() str="Program to split strings using custom split function." char space split(str, seperator); cout <<' the split is: '; for (int < max; i++) << ' : ' strings[i]; return 0; pre> <p> <strong>Output</strong> </p> <pre> The split string is: i : 0 Program i : 1 to i : 2 split i : 3 strings i : 4 using i : 5 custom i : 6 split i : 7 function. </pre> <h3>Use std::getline() function to split string</h3> <p>A getline() function is a standard library function of C++ used to read the string from an input stream and put them into the vector string until delimiter characters are found. We can use <strong>std::getline()</strong> function by importing the header file.</p> <p> <strong>Syntax</strong> </p> <pre> getline(str, token, delim); </pre> <p>It has three parameters:</p> <p> <strong>str:</strong> A str is a variable that stores original string.</p> <p> <strong>token:</strong> It stores the string tokens extracted from original string.</p> <p> <strong>delim:</strong> It is a character that are used to split the string. For example, comma (,), space ( ), hyphen (-), etc.</p> <h3>Program to use getline() function to split strings</h3> <p>Let's consider an example to split strings using the getline() function in C++.</p> <p> <strong>Program3.cpp</strong> </p> <pre> #include #include #include #include using namespace std; int main() { string S, T; // declare string variables getline(cin, S); // use getline() function to read a line of string and store into S variable. stringstream X(S); // X is an object of stringstream that references the S string // use while loop to check the getline() function condition while (getline(X, T, ' ')) { /* X represents to read the string from stringstream, T use for store the token string and, ' ' whitespace represents to split the string where whitespace is found. */ cout << T << endl; // print split string } return 0; } </pre> <p> <strong>Output</strong> </p> <pre> Welcome to the JavaTpoint and Learn C++ Programming Language. Welcome to the JavaTpoint and Learn C++ Programming Language. </pre> <h3>Program to split the given string using the getline() function</h3> <p>Let's consider an example to split a given string in C++ using the getline() function.</p> <p> <strong>Program4.cpp</strong> </p> <pre> #include #include #include #include void split_str( std::string const &str, const char delim, std::vector &out ) { // create a stream from the string std::stringstream s(str); std::string s2; while (std:: getline (s, s2, delim) ) { out.push_back(s2); // store the string in s2 } } int main() { std:: string s2 = 'Learn How to split a string in C++'; const char delim = ' '; /* define the delimiter like space (' '), comma (,), hyphen (-), etc. */ std::cout << 'Your given string is: ' << s2; std::vector out; // store the string in vector split_str (s2, delim, out); // call function to split the string // use range based for loop for (const auto &s2: out) { std::cout << ' ' << s2; } return 0; } </pre> <p> <strong>Output</strong> </p> <pre> Your given string is: Learn How to split a string in C++ Learn How to split a string in C++ </pre> <h3>Use find() and substr() function to split strings</h3> <p>Let's write a program to use find() function and substr() function to split given strings in C++.</p> <p> <strong>Program4.cpp</strong> </p> <pre> #include #include using namespace std; int main() { // given string with delimiter string given_str = 'How_to_split_a_string_using_find()_and_substr()_function_in_C++'; string delim = '_'; // delimiter cout << ' Your string with delimiter is: ' << given_str << endl; size_t pos = 0; string token1; // define a string variable // use find() function to get the position of the delimiters while (( pos = given_str.find (delim)) != std::string::npos) { token1 = given_str.substr(0, pos); // store the substring cout << token1 << endl; given_str.erase(0, pos + delim.length()); /* erase() function store the current positon and move to next token. */ } cout << given_str << endl; // it print last token of the string. } </pre> <p> <strong>Output</strong> </p> <pre> Your string with delimiter is: How_to_split_a_string_using_find()_and_substr()_function_in_C++ How to split a string using find() and substr() function in C++ </pre> <p>In the above program, we use a <strong>find()</strong> function inside the loop to repeatedly find the occurrence of the delimiter in the given string and then split it into tokens when the delimiter occurs. And the <strong>substr()</strong> function stores the sub-string to be printed. On the other hand, an erase() function stores the current position of the string and moves to the next token, and this process continues until we have got all the split strings.</p> <hr></=></pre></endl;>
Programa, skirta naudoti pasirinktinę split() funkciją, kad padalintų eilutes
Parašykime programą, skirtą suskaidyti eilučių sekas C++, naudodami pasirinktinę split() funkciją.
Program2.cpp
#include #include #define max 8 // define the max string using namespace std; string strings[max]; // define max string // length of the string int len(string str) { int length = 0; for (int i = 0; str[i] != ' '; i++) { length++; } return length; } // create custom split() function void split (string str, char seperator) { int currIndex = 0, i = 0; int startIndex = 0, endIndex = 0; while (i <= len(str)) { if (str[i]="=" seperator || i="=" endindex="i;" string substr ; substr.append(str, startindex, - startindex); strings[currindex]="subStr;" currindex +="1;" startindex="endIndex" 1; } i++; int main() str="Program to split strings using custom split function." char space split(str, seperator); cout <<\' the split is: \'; for (int < max; i++) << \' : \' strings[i]; return 0; pre> <p> <strong>Output</strong> </p> <pre> The split string is: i : 0 Program i : 1 to i : 2 split i : 3 strings i : 4 using i : 5 custom i : 6 split i : 7 function. </pre> <h3>Use std::getline() function to split string</h3> <p>A getline() function is a standard library function of C++ used to read the string from an input stream and put them into the vector string until delimiter characters are found. We can use <strong>std::getline()</strong> function by importing the header file.</p> <p> <strong>Syntax</strong> </p> <pre> getline(str, token, delim); </pre> <p>It has three parameters:</p> <p> <strong>str:</strong> A str is a variable that stores original string.</p> <p> <strong>token:</strong> It stores the string tokens extracted from original string.</p> <p> <strong>delim:</strong> It is a character that are used to split the string. For example, comma (,), space ( ), hyphen (-), etc.</p> <h3>Program to use getline() function to split strings</h3> <p>Let's consider an example to split strings using the getline() function in C++.</p> <p> <strong>Program3.cpp</strong> </p> <pre> #include #include #include #include using namespace std; int main() { string S, T; // declare string variables getline(cin, S); // use getline() function to read a line of string and store into S variable. stringstream X(S); // X is an object of stringstream that references the S string // use while loop to check the getline() function condition while (getline(X, T, ' ')) { /* X represents to read the string from stringstream, T use for store the token string and, ' ' whitespace represents to split the string where whitespace is found. */ cout << T << endl; // print split string } return 0; } </pre> <p> <strong>Output</strong> </p> <pre> Welcome to the JavaTpoint and Learn C++ Programming Language. Welcome to the JavaTpoint and Learn C++ Programming Language. </pre> <h3>Program to split the given string using the getline() function</h3> <p>Let's consider an example to split a given string in C++ using the getline() function.</p> <p> <strong>Program4.cpp</strong> </p> <pre> #include #include #include #include void split_str( std::string const &str, const char delim, std::vector &out ) { // create a stream from the string std::stringstream s(str); std::string s2; while (std:: getline (s, s2, delim) ) { out.push_back(s2); // store the string in s2 } } int main() { std:: string s2 = 'Learn How to split a string in C++'; const char delim = ' '; /* define the delimiter like space (' '), comma (,), hyphen (-), etc. */ std::cout << 'Your given string is: ' << s2; std::vector out; // store the string in vector split_str (s2, delim, out); // call function to split the string // use range based for loop for (const auto &s2: out) { std::cout << ' ' << s2; } return 0; } </pre> <p> <strong>Output</strong> </p> <pre> Your given string is: Learn How to split a string in C++ Learn How to split a string in C++ </pre> <h3>Use find() and substr() function to split strings</h3> <p>Let's write a program to use find() function and substr() function to split given strings in C++.</p> <p> <strong>Program4.cpp</strong> </p> <pre> #include #include using namespace std; int main() { // given string with delimiter string given_str = 'How_to_split_a_string_using_find()_and_substr()_function_in_C++'; string delim = '_'; // delimiter cout << ' Your string with delimiter is: ' << given_str << endl; size_t pos = 0; string token1; // define a string variable // use find() function to get the position of the delimiters while (( pos = given_str.find (delim)) != std::string::npos) { token1 = given_str.substr(0, pos); // store the substring cout << token1 << endl; given_str.erase(0, pos + delim.length()); /* erase() function store the current positon and move to next token. */ } cout << given_str << endl; // it print last token of the string. } </pre> <p> <strong>Output</strong> </p> <pre> Your string with delimiter is: How_to_split_a_string_using_find()_and_substr()_function_in_C++ How to split a string using find() and substr() function in C++ </pre> <p>In the above program, we use a <strong>find()</strong> function inside the loop to repeatedly find the occurrence of the delimiter in the given string and then split it into tokens when the delimiter occurs. And the <strong>substr()</strong> function stores the sub-string to be printed. On the other hand, an erase() function stores the current position of the string and moves to the next token, and this process continues until we have got all the split strings.</p> <hr></=>
Norėdami padalinti eilutę, naudokite funkciją std::getline().
Funkcija getline () yra standartinė C++ bibliotekos funkcija, naudojama nuskaityti eilutę iš įvesties srauto ir įdėti jas į vektorinę eilutę, kol randami skiriamieji simboliai. Galime naudoti std::getline() funkcija importuojant antraštės failą.
Sintaksė
getline(str, token, delim);
Jis turi tris parametrus:
str: Str yra kintamasis, kuriame saugoma pradinė eilutė.
niūrus vidurkis
tokenas: Jame saugomi eilutės žetonai, išgauti iš originalios eilutės.
Dalintis: Tai simbolis, naudojamas skaidyti eilutę. Pavyzdžiui, kablelis (,), tarpas ( ), brūkšnelis (-) ir kt.
Programa, skirta naudoti getline() funkciją, kad padalintų eilutes
Panagrinėkime pavyzdį, kaip padalinti eilutes naudojant funkciją getline() C++.
Program3.cpp
#include #include #include #include using namespace std; int main() { string S, T; // declare string variables getline(cin, S); // use getline() function to read a line of string and store into S variable. stringstream X(S); // X is an object of stringstream that references the S string // use while loop to check the getline() function condition while (getline(X, T, ' ')) { /* X represents to read the string from stringstream, T use for store the token string and, ' ' whitespace represents to split the string where whitespace is found. */ cout << T << endl; // print split string } return 0; }
Išvestis
Welcome to the JavaTpoint and Learn C++ Programming Language. Welcome to the JavaTpoint and Learn C++ Programming Language.
Programa, skirta padalyti nurodytą eilutę naudojant funkciją getline().
Panagrinėkime pavyzdį, kaip padalyti nurodytą eilutę C++ naudojant funkciją getline().
Program4.cpp
#include #include #include #include void split_str( std::string const &str, const char delim, std::vector &out ) { // create a stream from the string std::stringstream s(str); std::string s2; while (std:: getline (s, s2, delim) ) { out.push_back(s2); // store the string in s2 } } int main() { std:: string s2 = 'Learn How to split a string in C++'; const char delim = ' '; /* define the delimiter like space (' '), comma (,), hyphen (-), etc. */ std::cout << 'Your given string is: ' << s2; std::vector out; // store the string in vector split_str (s2, delim, out); // call function to split the string // use range based for loop for (const auto &s2: out) { std::cout << ' ' << s2; } return 0; }
Išvestis
Oracle lentelės kūrimas
Your given string is: Learn How to split a string in C++ Learn How to split a string in C++
Norėdami padalinti eilutes, naudokite funkcijas find() ir substr().
Parašykime programą, kuri naudotų Find() funkciją ir substr() funkciją, kad padalitų nurodytas eilutes C++.
Program4.cpp
#include #include using namespace std; int main() { // given string with delimiter string given_str = 'How_to_split_a_string_using_find()_and_substr()_function_in_C++'; string delim = '_'; // delimiter cout << ' Your string with delimiter is: ' << given_str << endl; size_t pos = 0; string token1; // define a string variable // use find() function to get the position of the delimiters while (( pos = given_str.find (delim)) != std::string::npos) { token1 = given_str.substr(0, pos); // store the substring cout << token1 << endl; given_str.erase(0, pos + delim.length()); /* erase() function store the current positon and move to next token. */ } cout << given_str << endl; // it print last token of the string. }
Išvestis
Your string with delimiter is: How_to_split_a_string_using_find()_and_substr()_function_in_C++ How to split a string using find() and substr() function in C++
Aukščiau pateiktoje programoje naudojame a rasti () funkcija ciklo viduje, kad pakartotinai surastų skirtuko atsiradimą nurodytoje eilutėje ir tada, kai atsiranda skiriamoji priemonė, padalintų ją į žetonus. Ir substr() funkcija išsaugo spausdintiną antrinę eilutę. Kita vertus, funkcija erase() išsaugo dabartinę eilutės padėtį ir pereina prie kito prieigos rakto, o šis procesas tęsiasi tol, kol gauname visas padalintas eilutes.
=>