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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:23:02+00:00 2026-06-03T20:23:02+00:00

SOLUTION Thanks to 1111… vector<std::string> split_at_line(string str, int lines) { vector<std::string> nine_ln_strs; string temp;

  • 0

SOLUTION Thanks to 1111…

vector<std::string> split_at_line(string str, int lines) {
 vector<std::string> nine_ln_strs;
 string temp;
 stringstream ss;
 int i = 0;
 while(i != str.length()) {
     ss << str.at(i);
     if(i == lines) {
        lines += lines;
        getline(ss,temp);
        nine_ln_strs.push_back(temp);
        ss.clear();
        temp.clear();
     }
     if(i+lines > str.length()) {
        getline(ss,temp);
        nine_ln_strs.push_back(temp);
        ss.clear();
        temp.clear();
        break;
     }
     i++;
 }
 return nine_ln_strs;

}

===========================================

I was trying to practice and learn how to work with multidimensional arrays today and I came to a problem. I have no idea how I can split a string into multiple strings, every Nth line. I have searched the web but it seems there is only examples for white-spaces, and tokens. Is there anyway to do what I want to do?

Example:

#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
using namespace std;

const int five = 5;
int test[][five] = {

{ 0, 0, 1, 0, 0 },
{ 0, 1, 1, 0, 0 },
{ 0, 2, 1, 0, 0 }
};

int main() {
stringstream result;
int a = sizeof test / sizeof ( test[0] );
cout << a << endl;
int b = 5;
for ( int i = 0; i < a; i++ ) {
    for ( int j = 0, inc = 0 ; j < b; j++, inc++ ) {
        if(inc == 2) {
            result << hex << setfill ('0') << setw(4) << (int)test[i][j];
        } else {
            result << hex << setfill ('0') << setw(2) << (int)test[i][j];
        }
    }
}

string s = result.str();
cout << s << endl;

// split the string into segments of every 000000000000 and store them into a new string each time, or another array

   int z;       // hold
   cin >> z;

   return 0;
}
  • 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-03T20:23:03+00:00Added an answer on June 3, 2026 at 8:23 pm

    There might be a nicer way of doing it but this should work

     std::vector<std::string> split_at_line(const std::string& str, unsigned lines) {
         std::vector<std::string> nine_ln_strs;
         std::istringstream ss(str);
         std::string temp, t2;
    
         while(ss) { //whilst there is still data
             //get nine lines
             for(unsigned i=0; i!=lines && std::getline(ss, t2); ++i) {          
                 temp+=t2;
             }
             //add block to container
             nine_ln_strs.push_back(temp));
         }
         //return container
         return nine_ln_strs;
     }
    

    PS: C array aren’t very nice try and use containers where possible. Also making a constant called five defined to 5 isn’t very helpful. The idea of using a variable here is so you can change it later, if you changed the value of five to 6 then that would just be weird. test_sz would be a better name.

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

Sidebar

Related Questions

Here is the Solution ,thanks Gustavo for the suggestion: class Program { static void
Chosen Solution Thanks for the help everyone. I've decided to do the following. public
(see my answer below for solution - thanks for the feedback) It's probably something
Solution: thanks to the suggestion from @Guarav I was inspired and FINALLY came up
SOLUTION Thanks to casperOne's answer , here is my resulting function: Shared Function FormatDate(ByVal
SOLUTION: Thanks to Patrick below, I have refactored the C# CodeProject version into a
SOLUTION Thanks to Arman's P. proof of concept, finally got it to work with
Possible Duplicate: Convert a long hex string in to int array with sscanf I
UPDATE - Solution Thanks to jacobm for his help, I came up with a
Anyone got any neat solutions to prevent a Silverlight ChildWindow being moved? thanks, Mark

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.