Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8919247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:58:15+00:00 2026-06-15T05:58:15+00:00

I am trying to split a comma-separated string and then perform some action on

  • 0

I am trying to split a comma-separated string and then perform some action on each token, but ignoring duplicates, so sth. along the following lines:

int main(int, char**)
{
   string text = "token, test   string";

  char_separator<char> sep(", ");
  tokenizer< char_separator<char> > tokens(text, sep);
  // remove duplicates from tokens?
  BOOST_FOREACH (const string& t, tokens) {
    cout << t << "." << endl;
  }
}

Is there a way to do this on the boost::tokenizer?

I know that I can solve this problem using boost::split and std::unique, but was wondering whether there is a way to achieve this with the tokenizer as well.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T05:58:17+00:00Added an answer on June 15, 2026 at 5:58 am

    boost.tokenizer can do many cool things, but it cannot do this, the answer is indeed “no”.

    If you’re only looking to drop adjacent duplicates, boost.range can help make it seemless:

    #include <iostream>
    #include <string>
    #include <boost/range/adaptor/uniqued.hpp>
    #include <boost/foreach.hpp>
    #include <boost/tokenizer.hpp>
    
    using namespace boost;
    using namespace boost::adaptors;
    int main()
    {
        std::string text = "token, test   string test, test   test";
    
        char_separator<char> sep(", ");
        tokenizer< char_separator<char> > tokens(text, sep);
        BOOST_FOREACH (const std::string& t, tokens | uniqued ) {
            std::cout << t << "." << '\n';
        }
    }
    

    This prints:

    token.
    test.
    string.
    test.
    

    In order to do some action only on globally unique tokens, you will need to store state, one way or another. The simplest solution is probably an intermediate set:

    char_separator<char> sep(", ");
    tokenizer< char_separator<char> > tokens(text, sep);
    std::set<std::string> unique_tokens(tokens.begin(), tokens.end());
    BOOST_FOREACH (const std::string& t, unique_tokens) {
            std::cout << t << "." << '\n';
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As the title suggests: I'm trying to split sentences into either a comma-separated string
I'm trying to split a string on its punctuation, but the string may contain
I am trying to convert a comma separated string into an array using the
I am trying to split a string up but am having a problem in
I am trying to figure out how to take a comma-separated string as input
Trying to split this string 主楼怎么走 into separate characters (I need an array) using
I am trying to split a string in javascript . it works fine in
I am trying to split a string in Javascript using a Regular Expression. My
I'm working with an external REST API that returns some data as a comma-separated
I'm trying to get a PHP script to split a list of numbers separated

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.