Creating single string from array of strings C++

Started by
18 comments, last by Juliean 4 years ago

Bregma said:

Just a suggestion on tokenizing the string.

#include <iostream>
#include <regex>
#include <string>
#include <vector>

using namespace std;

/*
 * This here is the tokenizer. Splits an input string on “I” characters into an arbitrary number
 * strings appended to the result vector.
 */
void
split_on_sep(string const&amp; src, vector<string>&amp; result)
{
  static const regex  marker{"[^|]+"};
  auto tokens_begin = sregex_iterator{src.begin(), src.end(), marker};
  auto tokens_end = sregex_iterator{};

  for (auto t = tokens_begin; t != tokens_end; ++t)
    result.push_back((*t).str());
}

/*
 * Test it.
 */
int
main()
{
  const  string dbstring{"one|two|three"};
  vector<string> result;
  
  split_on_sep(dbstring, result);

  for (auto const&amp; s: result)
    cout << s << "\n";
}

Here's the output.

$ g++ -std=c++14 -o strs strs.cpp &amp;&amp; ./strs
one
two
three

All I have to say is, “beautiful ❤️”

Advertisement

I'll try it out when I get home thank you

I think the problem I'm having with any of these functions… the results must end in FString formats.

You should always be able to construct an FString by using the char-constructor:

std::string stdString;
FString str(stdString.c_str());

This again incurs a whole copy of the strings content, which may or may not be acceptable for your situation. If yo are working with Unreal you are probably better off using the FString-interface for the whole string-manipulation. Check the documentation:

https://docs.unrealengine.com/en-US/API/Runtime/Core/Containers/FString/index.html
There appears to be all the functions to plainly concat and split that string. Only if you want to actually use the regex you most likely have to convert like above.

Performance isn't really an issue at this time. I'm a noob when it comes to C++ so that's like 99% of the issue. I'm still working on getting the split function right. I don't need it to break up the entire string. I only need it to split the string at the first delimiter (in this case I'm using “,” for mine) and then output the split and the remaining string. It must output into FString format.

I see FString contains a chop function and a find function, I have to figure out the syntax of how to set it up to do what I'm looking for. Once this is setup I can go back to humming along until the next coding challenge lol.

For instance: WTF?

My understanding is I need to find the delimiter and the do a leftchop and rightchop, and output the results of each respectively.

If the function requires a character as a parameter, you can't pass in a string because strings aren't characters. Also reading the documentation that is apparently not how you are supposed to use FindChar, so you want something like

int pos;
char separator=',';
UE4Str.FindChar(separator,pos);
If I do not include declspec(noreturn) the compiler demands a return value (which creates a “return” output on the node which I don't actually want… I just want output of choppedstring and the remainingstring. If I include __declspec(noreturn) in the header on the function it kicks back another error.
The function is now doing what I want… other than having a return value. I need to properly remove the return and it's golden for what I need it to do. EDIT: THESOLUTION = changing it from “FString UClass::Function” to “void Uclass::Function” - void does not return. I hope this helps someone else lol. Thanks for all the help everyone!

__declspec(noreturn) has nothing to do with a return value, but tells the compiler that the function does not reach its return statement. This mostly applies to functions that terminate the program, or are guaranteed to throw an exception. So mostly stuff for low-level library writers to silence warnings and slightly allow the compiler to optimize certain paths of execution. Nothing you should ever need, and you need to be carefull with that because it can cause your program to behave weird (if a function marked noreturn actually returns, its undefined behaviour).

This topic is closed to new replies.

Advertisement