0

I am pretty new to C++ and that's why I need some help.

In Python I would do it this way:

myString = "test|arg" myArg = myString.split("|")[0] 

But now I am in C++ and I am having a char and want to kinda remove a special part.

#include "stdafx.h" #include <iostream> #include <boost/algorithm/string.hpp> bool isPartOf(const std::string& word, const std::string& sentence) { return sentence.find(word) != std::string::npos; } int _tmain(int argc, _TCHAR* argv[]) { char* myChar= "test|me"; if (isPartOf("|me", myChar)) { std::string sStr(myChar); boost::replace_all(sStr, "|me", ""); std::copy(sStr.begin(), sStr.end(), myChar); myChar[sStr.size()] = '\0'; printf(myChar); system("pause>nul"); } } 

That's my current code but I get this error:

Error 1 error C4996: 'std::_Copy_impl': Function call with parameters that >may be unsafe - this call relies on the caller to check that the passed values >are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See >documentation on how to use Visual C++ 'Checked Iterators' c:\program files >(x86)\vc\include\xutility 2132 1 ConsoleApplication2

I hope someone could help me

Best regards

2
  • in c++ you would maybe use std::string instead of a char* Commented Apr 26, 2016 at 8:53
  • Your code overwrites a literal string. You must not do that. Commented Apr 26, 2016 at 9:18

1 Answer 1

2

C++ equivalent to your Python program:

#include <string> #include <iostream> int main(void) { std::string myString = "test|me"; std::string myArg = myString.substr(0, myString.find('|')); std::cout << myArg << std::endl; } 

output

test 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.