0
using namespace std;
#include <iostream>
#include <vector>
#include <string>
std::vector<std::string> DIFSplitStringByNumber(const std::string& str, int splitLength)
{
    int NumSubstrings = str.length() / splitLength;
    std::vector<std::string> ret;



    for (auto i = 0; i < NumSubstrings; i++)
    {
        ret.push_back(str.substr(i * splitLength, splitLength));
    }

    // If there are leftover characters, create a shorter item at the end.
    if (str.length() % splitLength != 0)
    {
        ret.push_back(str.substr(splitLength * NumSubstrings));
    }


    return ret;
}

int main() {

    std::cout << DIFSplitStringByNumber("hello", 2);
}

This is my code. I found it on another question on how to split a string by every nth character. What I want to do now is to print out the split character but I dont know how. I know cout doesn't work.

Tinagaran
  • 39
  • 8
  • 1
    Loop over the vector and print the elements? – Mestkon May 21 '20 at 16:15
  • One way: tterate over the elements of the vector and write each to `std::cout`. – R Sahu May 21 '20 at 16:15
  • 1
    Does this answer your question? [How to print out the contents of a vector?](https://stackoverflow.com/questions/10750057/how-to-print-out-the-contents-of-a-vector) – Andro May 21 '20 at 16:15
  • 1
    Write an `operator<<` for whatever you want to print. There's no magic in C++ that lets you just print arbitrary stuff. You have to specify how you want it to be printed. You write code to do that. – Jesper Juhl May 21 '20 at 16:32

0 Answers0