answersLogoWhite

0


Best Answer

#include<iostream>

#include<vector>

#include<string>

std::vector<std::string> parse (const std::string& s, const char delim)

{

std::vector<std::string> result {};

auto start = 0U;

auto end = s.find (delim);

while (end != s.npos)

{

result.push_back (s.substr(start, end - start));

start = ++end;

end = s.find (delim, start);

}

result.push_back (s.substr (start, s.npos - start));

return result;

}

std::vector<std::string> parse (const std::string& s, const std::string& delim)

{

std::vector<std::string> result {};

auto start = 0U;

auto end = s.find (delim);

while (end != s.npos)

{

result.push_back (s.substr(start, end - start));

start = end + delim.length();

end = s.find (delim, start);

}

result.push_back (s.substr (start, s.npos - start));

return result;

}

int main()

{

std::string str1 = "This is a string that will be parsed by a single-space delimiter.";

std::string str2 = "This==is==a==string==that==will==be==parsed==by==equal==operator.";

std::string str3 = "This string has no delimiter.";

std::cout << str1 << std::endl;

std::vector<std::string> v1 = parse (str1, ' ');

for (auto i : v1 )

std::cout << i << std::endl;

std::cout << std::endl;

std::cout << str2 << std::endl;

std::vector<std::string> v2 = parse (str2, "==");

for (auto i : v2 )

std::cout << i << std::endl;

std::cout << std::endl;

std::cout << str3 << std::endl;

std::vector<std::string> v3 = parse (str3, '\\');

for (auto i : v3 )

std::cout << i << std::endl;

std::cout << std::endl;

}

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you split a string in delimit c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How can you get a specific string from large string using c plus plus strings?

std::string::substr();


What is the difference between a C plus plus string and a C-style string?

A std::string is an object that encapsulates an array of type char whereas a C-style string is a primitive array with no members. A std::string is guaranteed to be null-terminated but a C-style string is not.


C plus plus library functions for string operations?

You can use "string" class in C++ for string operations or you may use c style string functions as well. #include &lt;string&gt; String class in C++ provides all basic function to operate on strings. you may details descriptin at http://www.cplusplus.com/reference/string/string/


What is the program to create a string class which stores a string value in c plus plus?

C++ already provides a string class in the C++ standard template library. #include&lt;iostream&gt; #include&lt;string&gt; int main() { using namespace std; string s {"Hello world!"}; cout &lt;&lt; s &lt;&lt; endl; }


C plus plus program to count digit in a string?

Use the following function to count the number of digits in a string. size_t count_digits (const std::string&amp; str) { size_t count = 0; for (std::string::const_iterator it=str.begin(); it!=str.end(); ++it) { const char&amp; c = *it; if (c&gt;='0' &amp;&amp; c&lt;='9'); ++count; } return count; }

Related questions

How can you get a specific string from large string using c plus plus strings?

std::string::substr();


What is the difference between a C plus plus string and a C-style string?

A std::string is an object that encapsulates an array of type char whereas a C-style string is a primitive array with no members. A std::string is guaranteed to be null-terminated but a C-style string is not.


Is there a datatype string in c plus plus?

Yes.


C plus plus library functions for string operations?

You can use "string" class in C++ for string operations or you may use c style string functions as well. #include &lt;string&gt; String class in C++ provides all basic function to operate on strings. you may details descriptin at http://www.cplusplus.com/reference/string/string/


What is the role of plus operator between two string constant?

The plus operator between string constants allows string concatination: string a = "Hello, "; string b = "World!"; string c = a + b; The output of c would be: "Hello, World!".


What is the program to create a string class which stores a string value in c plus plus?

C++ already provides a string class in the C++ standard template library. #include&lt;iostream&gt; #include&lt;string&gt; int main() { using namespace std; string s {"Hello world!"}; cout &lt;&lt; s &lt;&lt; endl; }


How print the string with double cot in c plus plus language?

console.wrikerle("""");


How can you write a c plus plus program to display natural numbers from 1 to n?

void print_naturals (std::ostream&amp; os, unsigned max, char delimit) { for (int i=1; i&lt;=max; ++i) os &lt;&lt; i &lt;&lt; delimit; } Example usage: // Print list of first 100 naturals: print_naturals (std::cout, 100, '\n');


Why can't the c plus plus compiler find the string object?

The most likely reason that the C++ compiler can't find the string object is just that you've forgotten to include the string header file.Code Example:#include // so you can use C++ strings using namespace std; // so you can write 'string' instead of 'std::string' string sMyString; // declare a string


C program plus copy of string to another?

strcpy


Do I need to write a program to find a substring in a given string in c plus plus?

No.


C plus plus program to count digit in a string?

Use the following function to count the number of digits in a string. size_t count_digits (const std::string&amp; str) { size_t count = 0; for (std::string::const_iterator it=str.begin(); it!=str.end(); ++it) { const char&amp; c = *it; if (c&gt;='0' &amp;&amp; c&lt;='9'); ++count; } return count; }