fxduke
fxduke
CC#
Created by fxduke on 2/24/2023 in #help
❔ C++ Help
The code is:
vector<string> s_split(string Str,string Seperator) {
vector<string> SplitStrings;
int ArrayIndex = 0;
for (int Index = 0; Index < Str.length(); Index++) {
char Character = Str.at(Index);
string SeperationCharacter = Str.substr(Index,Seperator.length());
if (SeperationCharacter == Seperator) {
SplitStrings.insert(SplitStrings.begin() + ArrayIndex++,"");
} else {
SplitStrings[ArrayIndex] += Character;
}
}
return SplitStrings;
}
vector<string> s_split(string Str,string Seperator) {
vector<string> SplitStrings;
int ArrayIndex = 0;
for (int Index = 0; Index < Str.length(); Index++) {
char Character = Str.at(Index);
string SeperationCharacter = Str.substr(Index,Seperator.length());
if (SeperationCharacter == Seperator) {
SplitStrings.insert(SplitStrings.begin() + ArrayIndex++,"");
} else {
SplitStrings[ArrayIndex] += Character;
}
}
return SplitStrings;
}
I'm trying to make something like lua has:
local bleh = "hello there how are you?"
local bleh2 = string.split(bleh," ")
print(bleh2[1]) -- "hello"
local bleh = "hello there how are you?"
local bleh2 = string.split(bleh," ")
print(bleh2[1]) -- "hello"
This was me trying to use the function:
int main() {
cout << "began\n";
vector<string> Hi = s_split("Hello there how are you doing today?"," ");
cout << Hi[0] << "\n";
cout << Hi[1] << "\n\n\n";
std::cout << "Hello World!\n";
return 0;
}
int main() {
cout << "began\n";
vector<string> Hi = s_split("Hello there how are you doing today?"," ");
cout << Hi[0] << "\n";
cout << Hi[1] << "\n\n\n";
std::cout << "Hello World!\n";
return 0;
}
The error I got was "Segment Fault". This is my 2nd day trying C++ so I'm mostly looking for what I've done wrong not how what I made was inefficient but I appreciate any feedback!
6 replies