I am using the following code to replace a string within a string.
The same code as below is working in Visual Studio 2012 but not in Eclipse, and I can't figure out why.
The error is about invalid arguments in the find and replace functions of std::string:
void ReplaceStringInPlace(std::string& subject, const std::string& search, const std::string& replace)
{
// handle error situations/trivial cases
if (search.length() == 0)
{
// searching for a match to the empty string will result in an infinite loop
return;
}
if (subject.length() == 0)
{
return; // nothing to match against
}
std::size_t pos = 0;
while ((pos = subject.find(search, pos)) != std::string::npos)
{
subject.replace(pos, search.length(), replace);
pos += replace.length();
}
}
The error message is as follows:
Invalid arguments '
Candidates are:
? find(const char *, ?, ?)
? find(const stlpmtx_std::basic_string<char,stlpmtx_std::char_traits<char>,stlpmtx_std::allocator<char>> &, ?)
? find(const char *, ?)
? find(char, ?)
'
Problem 1:-
Is there a different way to use these functions in Eclipse? What should I do to make the error go away?
Problem 2 :-
What does the ? sign mean in the error messages?
Related
I'm getting a string from a Post but the string comes like this:
?re=784D30879\u0026rr=POH0525\u0026tt=525.100000\u0026id=0958567C-20DC-44B4-9FD0-1AD13453DEBF4
And i want:
?re=784D30879&rr=POH0525&tt=525.100000&id=0958567C-20DC-44B4-9FD0-1AD13453DEBF4
I am using a function to replace characters in a String but it sends me the following error: a universal character name cannot designate a character in the basic character set
So I'm calling the function:
message = replaceChars(message, string("\u0026"), string("&"));
And this is the function:
string replaceChars(string stringToChange, const string& charToChange, const string& newChar)
{
size_t initialPosition = 0;
while((initialPosition= stringToChange.find(charToChange, initialPosition)) != string::npos)
{
stringToChange.replace(initialPosition, charToChange.length(), newChar);
initialPosition += newChar.length();
}
return stringToChange;
}
I cant understand the error.
You should use "\\u0026" instead of "\u0026"
I have an issue where I have a RegEx, [^/\&\?]+.\w{3,4}(?=([\?&].*$|$)), but I cannot get it to work with the function at [ 1 ] below.
[ 1 ] - http://doc.qt.io/qt-5/qregexp.html
This is the code I've tried:
QRegExp rx("[^/\\\\&\\?]+\\.\\w{3,4}(?=([\\?&].*$|$))", Qt::CaseInsensitive, QRegExp::RegExp);
std::ostringstream list;
int pos = 0;
while ((pos = rx.indexIn(url, pos)) != -1) {
list << rx.cap(1).toStdString();
pos += rx.matchedLength();
}
return list;
It's supposed to extract the filename off a URL, but just returns nothing instead. I'm not sure what's going wrong. Can someone please offer assistance? Thank you in advance.
Qt has QUrl class for parsing URL etc. And there is QUrl::fileName method:
QUrl url("http://qt-project.org/support/file.html");
// url.adjusted(RemoveFilename) == "http://qt-project.org/support/"
// url.fileName() == "file.html"
I try to resolve the circuit satisfiability problem reading the circuit from file (in the form presented in text visualizer-somehow dynamic). If my circuit is small my resolver work smooth (small means like <16-18 wires). If i get to 25-30 wires so 2^25-30 possibilities i encountered a problem with a violation of access. I tried to free memory every time i can. I tried to create a new pointer of my expression every time, but the access violation always occur.
^ How is this possible ?
int evalBoolExprForBinaryVector(char *expr, int n, int binaryVector[]){
// create boolean expression from logical expression
char* expression = (char*) malloc(sizeof(char) * strlen(expr) + 1);
strcpy(expression, expr);
for(int binaryVectorCounter=0; binaryVectorCounter<n; binaryVectorCounter++){
char* currentSearchedIdentifier = (char*) malloc(sizeof(char) * 10);
char* index =(char*) malloc(sizeof(char) * 10);
char* valueOfIndex = (char*) malloc(sizeof(char)*2);
strcpy(currentSearchedIdentifier,"v[");
sprintf(index, "%d", binaryVectorCounter);
strcat(currentSearchedIdentifier, index);
strcat(currentSearchedIdentifier, "]");
sprintf(valueOfIndex, "%d", binaryVector[binaryVectorCounter]);
expression = str_replace(expression,currentSearchedIdentifier,valueOfIndex);
free(currentSearchedIdentifier);
free(index);
free(valueOfIndex);
}
// here my expression will be something like
// ( 0 | 1 ) & (!0 | !1) & ...
// evaluate this
return evalBoolExpr(expression);
};
Here is my code for better understanding.
The program breaks with this exception in strlen.asm at:
main_loop:
mov eax,dword ptr [ecx] ; read 4 bytes
Thanks in advance for any thoughts.
I rewrite this part in c++ manner and everything worked smooth (some delay but at least it can finish with success)
void replaceAll(std::string& str, const std::string& from, const std::string& to) {
if(from.empty())
return;
size_t start_pos = 0;
while((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length();
}
}
int evalBoolExprForBinaryVector(char *expr, int n, int binaryVector[]){
std::string expression(expr);
for(int binaryVectorCounter=0; binaryVectorCounter<n; binaryVectorCounter++){
std::string currentSearchedIdentifier, valueOfIndex;
currentSearchedIdentifier = "v[" + std::to_string(binaryVectorCounter) + "]";
valueOfIndex = std::to_string(binaryVector[binaryVectorCounter]);
replaceAll(expression,currentSearchedIdentifier,valueOfIndex);
}
char *cstr = new char[expression.length() + 1];
strcpy(cstr, expression.c_str());
return evalBoolExpr(cstr);
};
I'm having a hard time trying to get an app to compile in Visual Studio 2013. I've solved a good amount of errors but I can't find a solution for the last one.
here it is:
void Application::setupRenderSystem() {
mState->dumpValues();
String val = mState->getStringValue("renderSystem");
RenderSystemList *renderSystems = mRoot->getAvailableRenderers();
RenderSystemList::iterator r_it;
bool renderSystemFound = false;
for (r_it = renderSystems->begin(); r_it != renderSystems->end(); r_it++) {
RenderSystem *tmp = *r_it;
std::string rName(tmp->getName());
// returns -1 if string not found
if ((int)rName.find(val) >= 0) {
mRoot->setRenderSystem(*r_it);
renderSystemFound = true;
break;
}
}
if (!renderSystemFound) {
OGRE_EXCEPT(0, "Specified render system (" + val + ") not found, exiting...", "Application")
}
}
Visual Studio indicates that the line RenderSystemList *renderSystems = mRoot->getAvailableRenderers(); is the problem, especially mRoot
Here is the error I get:
error C2440: 'initializing' : cannot convert from 'const Ogre::RenderSystemList' to 'Ogre::RenderSystemList *'
The getAvailableRenderers method doesn't return a pointer to a RenderSystemList. You'd want to have the value stored in a reference to a RenderSystemList:
RenderSystemList const& renderSystems = mRoot->getAvailableRenderers();
The Ogre API says:
const RenderSystemList & getAvailableRenderers (void)
So it returns a const reference and not a pointer.
Modify your code like this
const RenderSystemList &renderSystems = mRoot->getAvailableRenderers();
If i have a string as such
"I am not here... \n..Hello\n.\n....Whats happening"
I want to replace the above string so:
"I am not here... \n..Hello\n. \n....Whats happening"
^ Space added
Just a bit of a background on what im doing. Im using sendmail in C++ and \n.\n is End Of Message Equivalent of sendmail. I just created a class that uses sendmail to send mails. but obviously if the user from the outsite gives sendmail that command i want it to be removed. Here is my message function just incase.:
//Operator to add to the message
void operator<<(string imessage){
if (imessage != ""){ message += imessage; }
}
How would i go about doing this. Thanks in advance :D
This is my last version :)
This code handles the case mentioned by #Greg Hewgill
string& format_text(string& str)
{
const string::size_type dot_offset = 2;
string::size_type found_at_start = str.find("\n.\n"),
found_at = str.find("\n.\n");
if(found_at_start != string::npos)
str.insert(0, " ");
while(found_at != string::npos)
{
str.insert(found_at+dot_offset+1, " ");
found_at = str.find("\n.\n", found_at+1);
}
return str;
}
int main()
{
string text = ".\nn\n.\nn";
std::cout << format_text(text);
}
Look up String.find and String.replace
For example (not tested)
string endOfMessage = "\n.\n";
string replacement = "\n. \n";
size_t position;
while (position = message.find(endOfMessage))
{
message.replace(position, endOfMessage.length(), replacement);
}
This is derived from Dan McG's answer so upvote him ;)
string endOfMessage = "\n.\n";
string replacement = "\n. \n";
size_t position;
while (position = message.find(endOfMessage, position) != message.npos)
{
message.replace(position, endOfMessage.length(), replacement);
position += replacement.length();
}
Boost has Boost.Regex (a regular expression module). Might be overkill if this is the only replacement you need to do.
Use std::search and the insert method of sequence containers such as string, deque, or whatever you use to store the message text.
typedef std::string::iterator SIter; // or whatever container you use
static const char *end_seq = "\n.\n";
for ( SIter tricky_begin = msg.begin();
tricky_begin = std::search( tricky_begin, msg.end(), end_seq, end_seq+3 ),
tricky_begin != msg.end(); ) {
tricky_begin = msg.insert( tricky_begin + 2, ' ' );
}