Replace part of string in C++

Started by
46 comments, last by Servant of the Lord 12 years, 1 month ago
That warning is indicating your don't have C++0x enabled. The "-std=c++0x" is a compiler flag. Where you would add it depends on the IDE / toolchain you are using to build your programs.
Advertisement
Ok, I'm using Geany. So do I add it at the build commands?
It appears you would input them here, under "compile" and "build", unless you are using a Make file (in which case it would go in the Make file).
Ok I put it in the build command before "-Wall", which I no idea what it means btw so If someone could explain I would be grateful smile.png, and it does complie when I #include <regex>. I'll try washu's code and see the results.

Thanx for the info!


EDIT: Ok, now I a real problem with this line

auto const &tag : player;

It ALWAYS says it needs an initializer before ':' . I suppose this initializes tag as a PlayerInfo variable with the contents of the player variable. So, why does it need an initializer before ':' and what kind of initializer am I supposed to declare?
Google will tell you what -Wall means. You'll probably need to search for something like "gcc wall", the minus symbol actually inverts a search term (you'll get all the pages except those that mention "wall").

You should also include -Werror in your command line options.

As for that code, be more specific. Include the actual source and actual compiler message. Here is a minimal program:


#include <string>
#include <regex>
#include <map>

typedef std::map<std::string, std::string> PlayerInfo;

void replaceTags(std::string &tagged, const PlayerInfo &player) {
for(auto const& tag : player) {
std::regex rx(tag.first);
tagged = std::regex_replace(tagged, rx, tag.second);
}
}

int main() {

}
With RegEx, as I already mentioned, "The entire <regex> library doesn't yet work, even though it compiles fine."

Currently it's just a dummy implementation, with empty functions. Some other parts of the new standard library work fine though (like std::shared_ptr).
Thanx for the answers -Werror did the trick. The code compiles but the function doesn't seem to work as SotL noticed. So as I won't probably be able to understand through experience, can someone explain to me what this line does?

for(auto const& tag : player)

I mean it's not a regular for as there aren't three conditions but only one. So the loop just runs for all the keys of player? Does it just runs forever? Or do I have to just add the extra conditions?
http://en.wikipedia.org/wiki/C%2B%2B11#Range-based_for-loop

I mean it's not a regular for as there aren't three conditions but only one. So the loop just runs for all the keys of player? Does it just runs forever? Or do I have to just add the extra conditions?


There are actually two. Notice the ':' as a divider. 'player' is a container (an array, vector, map, or another container type that supports begin() and end()).

It's part of the new standard, and is equivalent to this (but easier to type and read):
for(float myFloat : myContainerOfFloats)
{
//...
}


This loops over each item in the container.

'auto' is a new C++11 variable type, which basically works like, "Whatever variable is assigned first, make 'auto' be that type of variable".


auto myInt = 27; //Auto is an int.
myInt = "sfsfsadfas"; //Can't reassign a string-literal to an int, it's already been told to be an int.

auto myFloat = 45.0f; //Auto is told to be a float.
myFloat = 'q'; //You can't assign a 'char' to a float. You already told the auto to be a float.

This topic is closed to new replies.

Advertisement