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 9030329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:27:48+00:00 2026-06-16T07:27:48+00:00

boost::algorithm::join provides a convenient join on std::vector<std::string> . How would you expand this functionality

  • 0

boost::algorithm::join provides a convenient join on std::vector<std::string>.

How would you expand this functionality to use std::vector<std::tuple<std::string,bool>> to surround the results with single-quotes (for strings) if true, before doing the join.

This is not hard to do with loops, but I am looking for a solution that makes the most use of standard algorithms and C++11 features (eg lambdas).

Continue to use boost’s join, if feasible: elegance/readability/conciseness are more important.

CODE

#include <string>
#include <vector>
#include <tuple>
#include <boost/algorithm/string/join.hpp> 

int main( int argc, char* argv[] )
{
  std::vector<std::string> fields = { "foo", "bar", "baz" };
  auto simple_case = boost::algorithm::join( fields, "|" );

  // TODO join surrounded by single-quotes if std::get<1>()==true
  std::vector<std::tuple< std::string, bool >> tuples =
   { { "42", false }, { "foo", true }, { "3.14159", false } };

  // 42|'foo'|3.14159 is our goal
}

EDIT

OK, I took kassak’s suggestion below and took a look at boost::transform_iterator() – I got put off by the verbosity of the example in boost’s own documentation, so I tried std::transform() – it’s not as short as I wanted, but it seems to work.

ANSWER

#include <string>
#include <vector>
#include <tuple>
#include <iostream>
#include <algorithm>
#include <boost/algorithm/string/join.hpp> 

static std::string
quoted_join( 
    const std::vector<std::tuple< std::string, bool >>& tuples, 
    const std::string& join
)
{
    std::vector< std::string >  quoted;
    quoted.resize( tuples.size() );
    std::transform( tuples.begin(), tuples.end(), quoted.begin(),
        []( std::tuple< std::string, bool > const& t ) 
        {
            return std::get<1>( t ) ? 
                "'" + std::get<0>(t) + "'" :
                std::get<0>(t);
        }
    );
  return boost::algorithm::join( quoted, join );
}

int main( int argc, char* argv[] )
{
  std::vector<std::tuple< std::string, bool >> tuples =
  { 
    std::make_tuple( "42", false ), 
    std::make_tuple( "foo", true ), 
    std::make_tuple( "3.14159", false ) 
  };

  std::cerr << quoted_join( tuples, "|" ) << std::endl;
}
  • 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-16T07:27:49+00:00Added an answer on June 16, 2026 at 7:27 am

    If you want to use join, you could wrap collection in boost::transform_iterator and add quotes if needed

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <boost/algorithm/string.hpp> #include <vector> #include <iostream> #include <string> using namespace std; using namespace boost;
Can't for the life of me understand why this fails: #include <vector> #include boost/algorithm/string/predicate.hpp
Compiling this example #include <boost/bind.hpp> #include <boost/algorithm/string.hpp> #include <algorithm> #include <iostream> #include <vector> using
I have a useful macro here: #include <algorithm> #include <vector> #include <string> #include <boost/algorithm/string.hpp>
This is my code: #include <string> #include <boost/algorithm/string/regex.hpp> string f(const string& s) { using
My code: #include <string> #include <boost/algorithm/string/regex.hpp> std::cout << boost::algorithm::replace_regex_copy( {x}{y}, // source string boost::regex(\\{.*?\\}),
I recently wanted to use boost::algorithm::join but I couldn't find any usage examples and
I tried to compile this code: #include <boost/range/adaptors.hpp> #include <boost/range/algorithm.hpp> #include <vector> int main()
I have a simple C++ with Boost like this: #include <boost/algorithm/string.hpp> int main() {
include #include <algorithm> #include<boost/algorithm/string.hpp> #include<boost/regex.hpp> using namespace std; using namespace boost; string _getBasehtttp(string url)

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.