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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:45:55+00:00 2026-06-05T06:45:55+00:00

I tried to create a string splitting template like the following, I get errors.

  • 0

I tried to create a string splitting template like the following, I get errors.

struct splitX
{
  enum empties_t { empties_ok, no_empties };
};

template <typename Container>
Container& split(
  Container&                                 result,
  const typename Container::value_type&      s,
  typename Container::value_type::value_type delimiter,
  splitX::empties_t                           empties = splitX::empties_ok )
{
  result.clear();
  std::istringstream ss( s );
  while (!ss.eof())
  {
      typename Container::value_type field;
      getline( ss, field, delimiter );
      if ((empties == split::no_empties) && field.empty()) continue;
      result.push_back( field );
  }
  return result;
}

This fails with the error from the headline when I’m trying to use it with the vector class like below:

std::getline(myfile,line);
std::vector<std::string> fields;
split(fields,line,' ');

// Test split function
std::cout << line << std::endl;
for(int i = 0; i < fields.size();i++)
  std::cout << fields[i];
  • 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-05T06:45:56+00:00Added an answer on June 5, 2026 at 6:45 am

    Ok, Microsoft Visual C++ 2008 doesn’t like your Container::value_type::value_type.

    You can use this monstrous construction:

    template <typename T>
    struct vte /*value_type_extractor*/
    {
        typedef typename T::value_type type;
    };
    
    template <typename Container>
    Container& split(
       Container&                                 result,
       const typename Container::value_type&      s,
       typename vte<typename vte<Container>::type>::type delimiter,
       splitX::empties_t                           empties = splitX::empties_ok )
    {
      typedef typename Container::value_type string_type;
      typedef typename string_type::value_type elem_type;
      std::basic_istringstream<elem_type> ss( s );
      ...
    }
    

    Or, you can do it in STL style:

    #inlcude <iterator>
    
    template <typename OutputIterator, typename StringType>
    OutputIterator split(
      OutputIterator                  out,
      const StringType&               s,
      typename StringType::value_type delimiter,
      splitX::empties_t               empties = splitX::empties_ok )
    {
      typedef typename StringType::value_type elem_type;
      std::basic_istringstream<elem_type> ss( s );
      while (!ss.eof())
      {
          StringType field;
          std::getline( ss, field, delimiter );
          if ((empties == splitX::no_empties) && field.empty()) continue;
          *out++ = field;
      }
      return out;
    }
    
    //somewhere in code
    {
        ...
        split(std::back_inserter(your_container), line, ' ');
    }
    

    Or, you can stop pretending to be a template god and write two overloaded functions:

    template <typename Container>
    Container& split(
       Container&              result,
       const std::string&      s,
       char                    delimiter,
       splitX::empties_t       empties = splitX::empties_ok )
    {
      std::istringstream ss(s);
      ...
    }
    
    template <typename Container>
    Container& split(
       Container&              result,
       const std::wstring&     s,
       wchar_t                 delimiter,
       splitX::empties_t       empties = splitX::empties_ok )
    {
      std::wistringstream ss(s);
      ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried to create a singleton to set and get a string between different
I tried to create this by following the video at http://www.asp.net/learn/videos/video-7026.aspx where Joe Stagner
I tried to create a Style for DataGridTextColumn with the following code <Style TargetType={x:Type
I tried to create a file using Oracle UTL_FILE.FOPEN command but i get this
I tried to create a new Swing GUI desktop application template using NetBean 7.0
I have created the following view for users to create new items. Ive tried
Out of curiosity I tried to create a really large string. It turned out
I have the following string: http://www.google.ie/ .I want to create a string www.google.ie How
I'm really surprised about my JTextArea. Following JTextArea JAVAdoc i tried to create a
I referred to sample spring application on netbeans.org and tried to create a simple

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.