Click here to Skip to main content
15,395,897 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is the code:
string hey = "hello hello wassup";
std::vector<string> arr;
arr = Split(hey, ' ');

The split looks kind of like this:
std::vector<string> Split(string str, char chr) {
  ... stuff i know works
  std::vector<string> ret;
  ... code to add data to ret, which also works
  return ret;

I assume there is something wrong with using std::vector<string> as a function return. How do i fix this?

What I have tried:

I have tried assigning it std::vector<string> arr = Split(hey, ' '); and as described above, none of it works. Both gives me bad allocation error.
Updated 6-Dec-21 0:39am
Richard MacCutchan 6-Dec-21 6:20am
As with all your other questions, the error lies in the code you have written. But since we cannot see that code we cannot offer suggested solutions.

I have just taken the above code, added some simple split logic and tested it and it works fine. So the problem must be in the code that you describe as "stuff i know works".
CPallini 6-Dec-21 6:39am
I guess you are right.
Richard MacCutchan 6-Dec-21 8:49am
As your sample below clearly demonstrates.
There is something wrong in your assumptions. Try
#include <iostream>
#include <sstream>
#include <vector>

using namespace std;
std::vector<string> Split(string str);

int main()
  string hey = "hello hello wassup";
  vector<string> arr;
  arr = Split(hey);
  for (const auto & s : arr)
    cout << s << "\n";

vector<string> Split(string str)
  istringstream iss(str);
  vector<string> ret;
  while ( iss )
    string s;
    iss >> s;
    if ( ! s.empty() )
  return ret;
Member 14769677 6-Dec-21 6:51am
So it kind of works but also not, doesn't give the same error but now it doesn't display anything instead.
CPallini 6-Dec-21 6:53am
You should post your code, in order to get meaningful help.
Rick York 6-Dec-21 11:43am
I have a question for you : do you consider it better to return a vector of strings from the function instead of passing a reference to the vector as an argument to the function or are they essentially equivalent? With move semantics I can see how there would be little difference but I tend to prefer to pass a reference.
CPallini 7-Dec-21 2:18am
There is no performance penalty so I tend to use return.
I reckon it is another of the many, many C++ subtleties. You have trade off some esplicitness (if you pass a reference it very apparent what is going on) for code elegance.
Stefan_Lang 7-Dec-21 5:22am
I'm inclined to agree: the std implementation and return optimization is so good nowadays that it's hard to come up with a syntax that works even better.

Moreover, when suggesting code to a beginner, there's no point wasting time on such subtleties: it's better to clearly write what you intend rather than a subtle alternative that solves a problem that no one asked about.

(and have a 5!)
CPallini 7-Dec-21 6:37am
Thank you!
Anyway, even if I prefer the return syntax, I was referring to the very return syntax when I wrote 'subtlety', because it somewhat hides to the programmer what is really happening. The reference syntax, on the other hand, while possibly clumsy, shows the programmer what is going on, exactly.
Stefan_Lang 7-Dec-21 5:27am
The C++ compiler is very good at return optimization: if you construct an object at the start of a function and then return it at the end of the function, the compiler will not construct this object on the local stack of the function, but on the stack of the calling function - that way it doesn't need to be copied on return.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900