Open In App

C++ String to Vector Using Delimiter

Last Updated : 13 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Delimiters are used as separators between the characters or words in a string so that different results can get separated by the delimiter. In this article let us see the different ways in which a string with delimiters can be converted to a vector of words after getting separated by delimiters.

Example:

string x=”A B C”;

string y=”A*B*C”;

// Separate x into [‘A’,’B’,’C’] with delimiter ‘ ‘

// Separate y into [‘A’,’B’,’C’] with delimiter ‘*’

Methods that can be used to perform this operation are :

  • Using find() function
  • Using strtok() function
  • Using getline() and stringstream
  • Using find_first_not_of() with find() function
  • Using regex_token_iterator

1. Using find() function

The find() function is used to find the first occurrence of the substring a string and returns the index of that in the given string, if not found returns npos. To know more about npos refer to the article: string::npos in C++.

C++




// C++ Program for Conversion
// String To Vector using Delimiter
// using  find()
#include <bits/stdc++.h>
 
using namespace std;
 
// Function to split the string to words in a vector
// separated by the delimiter
vector<string> split(string str, string delimiter)
{
    vector<string> v;
    if (!str.empty()) {
        int start = 0;
        do {
            // Find the index of occurrence
            int idx = str.find(delimiter, start);
            if (idx == string::npos) {
                break;
            }
 
            // If found add the substring till that
            // occurrence in the vector
            int length = idx - start;
            v.push_back(str.substr(start, length));
            start += (length + delimiter.size());
        } while (true);
        v.push_back(str.substr(start));
    }
 
    return v;
}
 
int main()
{
    // Delimiter is " "
    string s = "GeeksforGeeks is a computer science portal";
    vector<string> res = split(s, " ");
 
    for (int i = 0; i < res.size(); i++) {
        cout << res[i] << endl;
    }
 
    // Delimiter is **
    s = "GeeksforGeeks**is**a**computer**science**portal";
    res = split(s, "**");
 
    for (int i = 0; i < res.size(); i++) {
        cout << res[i] << endl;
    }
 
    return 0;
}


Output

GeeksforGeeks
is
a
computer
science
portal
GeeksforGeeks
is
a
computer
science
portal

2. Using strtok() Function

The strtok() function returns the next token separated by a delimiter in a string. The tokens are pushed into the vector until the null pointer is reached.

C++




// C++ Program for Conversion
// String To Vector using Delimiter
// Using strtok() function
#include<bits/stdc++.h>
using namespace std;
  
vector<string> split(string str,   char* delimiter)
{
    vector<string> v;
     char *token = strtok(const_cast<char*>(str.c_str()), delimiter);
    while (token != nullptr)
    {
        v.push_back(string(token));
        token = strtok(nullptr, delimiter);
    }
   
  return v;
}
  
int main()
{
    // Delimiter is " "
    string s = "GeeksforGeeks is a computer science portal";
    vector<string> res = split(s, " ");
 
    for (int i = 0; i < res.size(); i++) {
        cout << res[i] << endl;
    }
    // Delimiter is **
    s = "GeeksforGeeks**is**a**computer**science**portal";
    res = split(s, "**");
 
    for (int i = 0; i < res.size(); i++) {
        cout << res[i] << endl;
    }
  
    return 0;
}


Output

GeeksforGeeks
is
a
computer
science
portal
GeeksforGeeks
is
a
computer
science
portal

3. Using getline() and stringstream

It works for single-character delimiters.

C++




// C++ Program for Conversion
// String To Vector using Delimiter
// Using getline() and stringstream
#include <bits/stdc++.h>
using namespace std;
 
vector<string> split(string str, char delimiter)
{
  // Using str in a string stream
    stringstream ss(str);
    vector<string> res;
    string token;
    while (getline(ss, token, delimiter)) {
        res.push_back(token);
    }
    return res;
}
 
int main()
{
    // Delimiter is " "
    string s = "GeeksforGeeks is a computer science portal";
    vector<string> res = split(s, ' ');
 
    for (int i = 0; i < res.size(); i++) {
        cout << res[i] << endl;
    }
    // Delimiter is *
    s = "GeeksforGeeks*is*a*computer*science*portal";
    res = split(s, '*');
 
    for (int i = 0; i < res.size(); i++) {
        cout << res[i] << endl;
    }
 
    return 0;
}


Output

GeeksforGeeks
is
a
computer
science
portal
GeeksforGeeks
is
a
computer
science
portal

 4. Using find_first_not_of() with find() function

find_first_not_of()  is used to search the string for the first character that does not match any of the characters specified in the string. The find() function is used to find the first occurrence of a character.

C++




// C++ Program for Conversion
// String To Vector using Delimiter
// Using find_first_not_of()
// with find() function
#include <bits/stdc++.h>
using namespace std;
 
vector<string> split(string str, char delimiter)
{
 
    vector<string> res;
    size_t first;
    size_t last = 0;
 
    while ((first = str.find_first_not_of(delimiter, last))
           != string::npos) {
        last = str.find(delimiter, first);
        res.push_back(str.substr(first, last - first));
    }
    return res;
}
 
int main()
{
    // Delimiter is " "
    string s = "GeeksforGeeks is a computer science portal";
    vector<string> res = split(s, ' ');
 
    for (int i = 0; i < res.size(); i++) {
        cout << res[i] << endl;
    }
    // Delimiter is *
    s = "GeeksforGeeks*is*a*computer*science*portal";
    res = split(s, '*');
 
    for (int i = 0; i < res.size(); i++) {
        cout << res[i] << endl;
    }
 
    return 0;
}


Output

GeeksforGeeks
is
a
computer
science
portal
GeeksforGeeks
is
a
computer
science
portal

 5. Using regex_token_iterator

The regex token iterator tokenizes the sentence based on the given regular expression.

C++




// C++ Program for Conversion
// String To Vector using Delimiter
// Using regex_token_iterator
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    // Delimiter is " "
    string s = "GeeksforGeeks is a computer science portal";
 
    regex reg("\\ ");
 
    vector<string> res1(
        sregex_token_iterator(s.begin(), s.end(), reg, -1),
        sregex_token_iterator());
 
    for (int i = 0; i < res1.size(); i++)
    {
        cout << res1[i] << endl;
    }
 
    return 0;
}


Output

GeeksforGeeks
is
a
computer
science
portal


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads