Peter Lapisu
Peter Lapisu

Reputation: 20975

Creating iterative string

is there a better / faster way to create string (std or (const) char *) created from string (const char *) and number (int), like

animation0, animation1, animation2 ... animation99

than this?

NOTE : doesnt have to use std, because hasValueForKey accepts const char *

std::stringstream strIter("animation0"); 

int i = 0;
while (hasValueForKey(strIter.str().c_str())) {

    // do some stuff

    ++i;
    strIter.str(std::string());
    strIter << "animation" << i;            
}

thanks

Upvotes: 1

Views: 177

Answers (1)

Constantinius
Constantinius

Reputation: 35039

well you could use the C99 API with snprintf(char *str, size_t size, const char *format, ...);:

int i = 0;
char str[50];
while (hasValueForKey(str)) {
    // do some stuff
    ++i;
    snprintf(str, 50, "animation%d", i);
}

Upvotes: 1

Related Questions