Sign in to follow this  
Drew_Benton

Unity Standard C++ Library Common Code Collaboration

Recommended Posts

Drew_Benton    1861
The other day I started messing around with making a scripting language just to see how I'd go about making one. As I was going though, I noticed a lot of my code could be greatly simplified with various stl functions that have been posted around GD. After using quite a lot of them, I figured it'd be nice to have them all in once place for further reference. So what follows is a little collection that I have so far. I have linked to where the original function was taken from. Now, I'm sure there are a lot of other neat useful functions to be added, so if you want to submit for this list, here is the template to follow. As you can see, html entities are removed so when you paste in your code, the tags are kept so it can easily be added to this post. Now, for the rules so this doesn't get too out of hand or unmanagable 1. If you don't like the standard C++ library, then this thread is where you can submit your comments. 2. Only functions that can be easily copied/pasted into a project and be explained. No classes or lots of complex code because they tend to be bigger need more explanations of how to use, and etc... For example, I have a templated manager class that would not belong here. There is also a great thread on encrypting strings using the TEA library. That code does not belong here either. 3. No Boost examples. Boost is a great library that probabally has a lot of functions that could make a lot of stuff presented here easier, but that's not the goal of this thread. I'll leave that at that. 4. Please fill out the template. Write a short test program that shows how the function works and save the output. This is to ensure accruate code is kept so people don't have problems trying to use the code presented here. If you take code from a post on GameDev, fill out the correct URL for reference. If you don't want to, well I'll end up doing it, but it'd be easier if you did [smile] 5. The issue of what to submit is kinda tricky. Somethings are obvious to some people and simply amazing to others. A good example is that a lot of people might not know there is a random_shuffle algorithm avaliable. So making a function that wraps that algoirhm so the user can just call Shuffle(vector) would be ok because of other alternatives posted on the net. The goal of this thread is to share modern knowledge of different things that can be useful to beginners though experts. This thread isn't meant to make up for people not knowing the Standard C++ Library, but rather as an encouragement to learn some of the more advanced things in it. Hopefully, it will be a good resource for a while! Thare are threads like this one that have great code, but unfortunately no one will probabally ever see again. For more examples of using the STL, take a look at this MSDN collection. There are many more as well, but code from sites like those should not be posted here. Also, if anyone sees anything that is wrong, or could possibly be done better, feel free to share. There are anchors used in this thread, so when you refer to code in it, you can use the anchor link to direct to a specific function. With that said...
Table of Contents: FileToString FileToVector lexer_merge_previous_append lexer_merge_previous_prepend lexer_merge_next_append lexer_merge_next_prepend ParseString RemoveAdjacentRepetitions TokenizeString TrimString StringContainsNone StringContainsOnly StringReplace StringToLower StringToUpper ToString
Title: StringReplace Description: Will replace "num_times" instances of "find_this" with "replace_with". If num_times is set to a negative number, all instances are replaced. URL: Original design Source:
void StringReplace(std::string& source, const std::string& find_this, const std::string& replace_with, int num_times = -1)
{
	int loc = 0;
	for(int i = 0; i != num_times; i++)
	{
		loc = source.find(find_this, 0);
		if (loc != -1) 
			source.replace(loc, find_this.length(), replace_with);
		else
			return;
	}
}

Example:
#include <iostream>
#include <string>

void StringReplace(std::string& source, const std::string& find_this, const std::string& replace_with, int num_times = -1)
{
	int loc = 0;
	for(int i = 0; i != num_times; i++)
	{
		loc = source.find(find_this, 0);
		if (loc != -1) 
			source.replace(loc, find_this.length(), replace_with);
		else
			return;
	}
}

int main(int argc, char* argv[])
{
	// Test 1 shows how to use without using the 'num_times'
	std::string source = "This is a test.";
	std::cout << source << std::endl;
	StringReplace(source, "i", "-");
	std::cout << source << std::endl << std::endl;

	// Test 2 shows how to use using 'num_times' as being 1
	source = "This is a test.";
	std::cout << source << std::endl;
	StringReplace(source, "i", "-", 1);
	std::cout << source << std::endl << std::endl;

	return 0;
}

Output:
This is a test.
Th-s -s a test.

This is a test.
Th-s is a test.

Press any key to continue

Title: FileToString Description: Will load a file with the given path of "filename" and return the contents of that file. If the file cannot be opened or does not exist, an empty string will be returned. URL: Original design Source:
std::string FileToString(const std::string &filename)
{
	std::ifstream fin(filename.c_str());
	return std::string((std::istreambuf_iterator<char>(fin)), std::istreambuf_iterator<char>());
}

Example:
#include <iostream>
#include <fstream>
#include <string>

std::string FileToString(const std::string &filename)
{
	std::ifstream fin(filename.c_str());
	return std::string((std::istreambuf_iterator<char>(fin)), std::istreambuf_iterator<char>());
}

int main(int argc, char* argv[])
{
	// Load this file into a string
	std::string myFile = FileToString("main.cpp");
	std::cout << myFile << std::endl;

	// Try and load a non-existant file
	myFile = FileToString("no-exist.zyx");
	if(myFile.size() == 0)
	{
		std::cout << "File is empty or does not exists" << std::endl;
	}

	return 0;
}

Output:
<< Main.cpp contents >>
File is empty or does not exists
Press any key to continue

Title: ParseString Description: This function will parse the "source" string into a vector of tokens based on spaces. Any group of tokens enclosed in 'token' are read as one token. URL: Original design Source:
std::vector<std::string> ParseString(const std::string& source, const char& token)
{
	using namespace std;
	vector<string> result;
	string item;
	stringstream ss(source);
	while(ss >> item)
	{
		if (item[0] == token) 
		{
			if(item[item.length() - 1] == token)
				result.push_back(item.substr(1, item.length()-2));
			else
			{
				string restOfItem;
				getline(ss, restOfItem, token);
				result.push_back(item.substr(1) + restOfItem);
			}
		}
		else
			result.push_back(item);
	}
	return result;
}

Example:
#include <iostream>
#include <vector>
#include <string>
#include <sstream>

std::vector<std::string> ParseString(const std::string& source, const char& token)
{
	using namespace std;
	vector<string> result;
	string item;
	stringstream ss(source);
	while(ss >> item)
	{
		if (item[0] == token) 
		{
			if(item[item.length() - 1] == token)
				result.push_back(item.substr(1, item.length()-2));
			else
			{
				string restOfItem;
				getline(ss, restOfItem, token);
				result.push_back(item.substr(1) + restOfItem);
			}
		}
		else
			result.push_back(item);
	}
	return result;
}

int main(int argc, char* argv[])
{
	std::string source = "-This is- a test of -how to parse- by token.";
	
	// Parse the string grouping wors by the '-' character
	std::vector<std::string> parsedTokens = ParseString(source, '-');

	for(int x = 0; x < parsedTokens.size(); x++)
		std::cout << parsedTokens[x] << std::endl;

	return 0;
}

Output:
This is
a
test
of
how to parse
by
token.
Press any key to continue

Title: TrimString Description: This function will remove all white space leading and following a string. URL: None Source:
void TrimString(std::string& source)
{
	int x = 0;
	for(x; x < source.size(); x++)
	{
		if(!isspace(source[x]))
			break;
	}
	source = source.substr(x);
	for(x = source.size() - 1; x >= 0; x--)
	{
		if(!isspace(source[x]))
			break;
	}
	source = source.substr(0, x + 1);
}

Example:
#include <iostream>
#include <string>

void TrimString(std::string& source)
{
	int x = 0;
	for(x; x < source.size(); x++)
	{
		if(!isspace(source[x]))
			break;
	}
	source = source.substr(x);
	for(x = source.size() - 1; x >= 0; x--)
	{
		if(!isspace(source[x]))
			break;
	}
	source = source.substr(0, x + 1);
}

int main(int argc, char* argv[])
{
	std::string source = "   This string needs trimming   ";
	std::cout << "Original string: " << source << std::endl;
	TrimString(source);
	std::cout << "Trimmed string: " << source << std::endl;
	return 0;
}

Output:
Original string:    This string needs trimming
Trimmed string: This string needs trimming
Press any key to continue

Title: StringContainsOnly Description: This function will return true if a string is only composed of the "validChars". It will return false if the string is composed of a character that is not part of the "validChars" string passed in URL: Original design Source:
bool StringContainsOnly(const std::string& source, const std::string& validChars)
{
	return (source.find_first_not_of(validChars) == std::string::npos);
}

Example:
#include <iostream>
#include <string>

bool StringContainsOnly(const std::string& source, const std::string& validChars)
{
	return (source.find_first_not_of(validChars) == std::string::npos);
}

int main(int argc, char* argv[])
{
	std::string source = "0123456789";
	if(StringContainsOnly(source, "13579"))
	{
		std::cout << "String only contains only odd digits" << std::endl;
	}
	else if(StringContainsOnly(source, "02468"))
	{
		std::cout << "String only contains even digits" << std::endl;
	}
	else if(StringContainsOnly(source, "0123456789"))
	{
		std::cout << "String only contains digits" << std::endl;
	}

	source = "13579";
	if(StringContainsOnly(source, "13579"))
	{
		std::cout << "String only contains only odd digits" << std::endl;
	}
	else if(StringContainsOnly(source, "02468"))
	{
		std::cout << "String only contains even digits" << std::endl;
	}
	else if(StringContainsOnly(source, "0123456789"))
	{
		std::cout << "String only contains digits" << std::endl;
	}

	return 0;
}

Output:
String only contains digits
String only contains only odd digits
Press any key to continue

Title: StringContainsNone Description: This function will return true of 'source' does not contain any of the characters in 'chars' or false otherwise. URL: None Source:
bool StringContainsNone(const std::string& source, const std::string& chars)
{
	return (source.find_first_of(chars) == std::string::npos);
}

Example:
#include <iostream>
#include <string>

bool StringContainsNone(const std::string& source, const std::string& chars)
{
	return (source.find_first_of(chars) == std::string::npos);
}

int main(int argc, char* argv[])
{
	std::string source = "abcdefghijklmnopqrstuvwxyz";
	
	if(StringContainsNone(source, "aeiou"))
	{
		std::cout << "String does not contain any vowels" << std::endl;
	}
	else
	{
		std::cout << "String contain vowels" << std::endl;
	}

	if(StringContainsNone(source, "bcdfghjklmnpqrstvwxyz"))
	{
		std::cout << "String does not contain any consonants" << std::endl;
	}
	else
	{
		std::cout << "String contains consonants" << std::endl;
	}

	source = "aei12";

	if(StringContainsNone(source, "aeiou"))
	{
		std::cout << "String does not contain any vowels" << std::endl;
	}
	else
	{
		std::cout << "String contains vowels" << std::endl;
	}

	if(StringContainsNone(source, "0123456789"))
	{
		std::cout << "String does not contain any digits" << std::endl;
	}
	else
	{
		std::cout << "String contains digits" << std::endl;
	}

	return 0;
}

Output:
String contain vowels
String contains consonants
String contains vowels
String contains digits
Press any key to continue

Title: TokenizeString Description: This function will tokenize a string based on the characters in the 'delim' string. It will return a vector of all of the tokens. URL: Original design Source:
std::vector<std::string> TokenizeString(const std::string& str, const std::string& delim)
{
	using namespace std;
	vector<string> tokens;
	size_t p0 = 0, p1 = string::npos;
	while(p0 != string::npos)
	{
		p1 = str.find_first_of(delim, p0);
		if(p1 != p0)
		{
			string token = str.substr(p0, p1 - p0);
			tokens.push_back(token);
		}
		p0 = str.find_first_not_of(delim, p1);
	}
	return tokens;
}

Example:
#include <iostream>
#include <string>
#include <vector>

std::vector<std::string> TokenizeString(const std::string& str, const std::string& delim)
{
	using namespace std;
	vector<string> tokens;
	size_t p0 = 0, p1 = string::npos;
	while(p0 != string::npos)
	{
		p1 = str.find_first_of(delim, p0);
		if(p1 != p0)
		{
			string token = str.substr(p0, p1 - p0);
			tokens.push_back(token);
		}
		p0 = str.find_first_not_of(delim, p1);
	}
	return tokens;
}

int main(int argc, char* argv[])
{
	std::string source = "This string will be tokenized by a vector of characters which are vowels";
	std::vector<std::string> tokens = TokenizeString(source, "aeiou");

	std::cout << source << std::endl;
	for(int x = 0; x < tokens.size(); x++)
	{
		std::cout << tokens[x] << std::endl;
	}

	std::cout << std::endl;

	source = "This.string.will; be tokenized, with  spaces and :puncutation!";
	tokens = TokenizeString(source, ",. :;!");
	std::cout << source << std::endl;
	for(int x = 0; x < tokens.size(); x++)
	{
		std::cout << tokens[x] << std::endl;
	}

	return 0;
}

Output:
This string will be tokenized by a vector of characters which are vowels
Th
s str
ng w
ll b
 t
k
n
z
d by
 v
ct
r
f ch
r
ct
rs wh
ch
r
 v
w
ls

This.string.will; be tokenized, with  spaces and :puncutation!
This
string
will
be
tokenized
with
spaces
and
puncutation
Press any key to continue

Title: FileToVector Description: This function will load a file and parse it based on the token into a vector. By default, it uses a newline. This function relies on the previous TokenizeString function. URL: None Source:
std::vector<std::string> FileToVector(const std::string& filename, const std::string& tokens = "\n")
{
	std::ifstream fin(filename.c_str());
	return TokenizeString(std::string((std::istreambuf_iterator<char>(fin)), std::istreambuf_iterator<char>()), tokens);
}

Example:
#include <iostream>
#include <string>
#include <fstream>
#include <vector>

std::vector<std::string> TokenizeString(const std::string& str, const std::string& delim)
{
	using namespace std;
	vector<string> tokens;
	size_t p0 = 0, p1 = string::npos;
	while(p0 != string::npos)
	{
		p1 = str.find_first_of(delim, p0);
		if(p1 != p0)
		{
			string token = str.substr(p0, p1 - p0);
			tokens.push_back(token);
		}
		p0 = str.find_first_not_of(delim, p1);
	}
	return tokens;
}

std::vector<std::string> FileToVector(const std::string& filename, const std::string& tokens = "\n")
{
	std::ifstream fin(filename.c_str());
	return TokenizeString(std::string((std::istreambuf_iterator<char>(fin)), std::istreambuf_iterator<char>()), tokens);
}

int main(int argc, char* argv[])
{
	// Load this file and tokenize it into a vector using 'a', 'e', 'i', 'o', and 'u'
	std::vector<std::string> tokens = FileToVector("main.cpp", "aeiou");
	for(int x = 0; x < tokens.size(); x++)
	{
		std::cout << tokens[x] << std::endl;
	}
	return 0;
}

Output:
<< Output >>

Title: lexer_merge_previous_append Description: This function will search a vector to see if any element is equal to 'token'. If it is, then that token will be removed from the vector, but it will be appended to the previous element in the vector. If there is a 'space' passed in, the contents of that variable will seperate the token from the end of the string of the previous element. You will have to call this multiple times for each 'token' you want. URL: None Source:
void lexer_merge_previous_append(std::vector<std::string>& source, const std::string& token, const std::string& space = "")
{
	std::vector<std::string>::iterator itr = std::find(source.begin(), source.end(), token);
	while(itr != source.end())
	{
		if(itr == source.begin())
			continue;
		std::string before = *(itr - 1);
		(itr - 1)->append(space);
		(itr - 1)->append(token);
		std::string after = *(itr - 1);
		source.erase(itr);
		itr = std::find(source.begin(), source.end(), token);
	}
}

Example:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

void lexer_merge_previous_append(std::vector<std::string>& source, const std::string& token, const std::string& space = "")
{
	std::vector<std::string>::iterator itr = std::find(source.begin(), source.end(), token);
	while(itr != source.end())
	{
		if(itr == source.begin())
			continue;
		std::string before = *(itr - 1);
		(itr - 1)->append(space);
		(itr - 1)->append(token);
		std::string after = *(itr - 1);
		source.erase(itr);
		itr = std::find(source.begin(), source.end(), token);
	}
}

int main(int argc, char* argv[])
{
	std::vector<std::string> source;
	source.push_back("int");
	source.push_back("*");
	source.push_back("var1");
	source.push_back(",");
	source.push_back("char");
	source.push_back("*");
	source.push_back("var2");

	// Show the original elemnts of the vector
	for(int x = 0; x < source.size(); x++)
	{
		std::cout << source[x] << " ";
	}
	std::cout << std::endl << std::endl;

	// Merge all '*' at the end of the previous element
	lexer_merge_previous_append(source, "*");

	// Show the new elemnts of the vector
	for(int x = 0; x < source.size(); x++)
	{
		std::cout << source[x] << " ";
	}
	std::cout << std::endl << std::endl;

	return 0;
}

Output:
int * var1 , char * var2

int* var1 , char* var2

Press any key to continue

Title: lexer_merge_previous_prepend Description: This function will search a vector to see if any element is equal to 'token'. If it is, then that token will be removed from the vector, but it will be prepended to the previous element in the vector. If there is a 'space' passed in, the contents of that variable will seperate the token from the start of the string of the previous element. URL: None Source:
void lexer_merge_previous_prepend(std::vector<std::string>& source, const std::string& token, const std::string& space = "")
{
	std::vector<std::string>::iterator itr = std::find(source.begin(), source.end(), token);
	while(itr != source.end())
	{
		if(itr == source.begin())
			continue;
		std::string newStr = token;
		newStr += space;
		newStr += (*(itr - 1));
		(*(itr - 1)) = newStr;
		source.erase(itr);
		itr = std::find(source.begin(), source.end(), token);
	}
}

Example:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

void lexer_merge_previous_prepend(std::vector<std::string>& source, const std::string& token, const std::string& space = "")
{
	std::vector<std::string>::iterator itr = std::find(source.begin(), source.end(), token);
	while(itr != source.end())
	{
		if(itr == source.begin())
			continue;
		std::string newStr = token;
		newStr += space;
		newStr += (*(itr - 1));
		(*(itr - 1)) = newStr;
		source.erase(itr);
		itr = std::find(source.begin(), source.end(), token);
	}
}

int main(int argc, char* argv[])
{
	std::vector<std::string> source;
	source.push_back("int");
	source.push_back("*");
	source.push_back("var1");
	source.push_back(",");
	source.push_back("char");
	source.push_back("*");
	source.push_back("var2");

	// Show the original elemnts of the vector
	for(int x = 0; x < source.size(); x++)
	{
		std::cout << source[x] << " ";
	}
	std::cout << std::endl << std::endl;

	// Merge all '*' at the start of the previous element
	lexer_merge_previous_prepend(source, "*");

	// Show the new elemnts of the vector
	for(int x = 0; x < source.size(); x++)
	{
		std::cout << source[x] << " ";
	}
	std::cout << std::endl << std::endl;

	return 0;
}

Output:
int * var1 , char * var2

*int var1 , *char var2

Press any key to continue

Title: lexer_merge_next_append Description: This function will search a vector to see if any element is equal to 'token'. If it is, then that token will be removed from the vector, but it will be appended to the next element in the vector. If there is a 'space' passed in, the contents of that variable will seperate the token from the end of the string of the next element. URL: None Source:
void lexer_merge_next_append(std::vector<std::string>& source, const std::string& token, const std::string& space = "")
{
	std::vector<std::string>::iterator itr = std::find(source.begin(), source.end(), token);
	while(itr < (source.end() - 1))
	{
		(itr + 1)->append(space);
		(itr + 1)->append(token);
		source.erase(itr);
		itr = std::find(source.begin(), source.end(), token);
	}
}

Example:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

void lexer_merge_next_append(std::vector<std::string>& source, const std::string& token, const std::string& space = "")
{
	std::vector<std::string>::iterator itr = std::find(source.begin(), source.end(), token);
	while(itr < (source.end() - 1))
	{
		(itr + 1)->append(space);
		(itr + 1)->append(token);
		source.erase(itr);
		itr = std::find(source.begin(), source.end(), token);
	}
}

int main(int argc, char* argv[])
{
	std::vector<std::string> source;
	source.push_back("int");
	source.push_back("*");
	source.push_back("var1");
	source.push_back(",");
	source.push_back("char");
	source.push_back("*");
	source.push_back("var2");

	// Show the original elemnts of the vector
	for(int x = 0; x < source.size(); x++)
	{
		std::cout << source[x] << " ";
	}
	std::cout << std::endl << std::endl;

	// Merge all '*' at the end of the next element
	lexer_merge_next_append(source, "*");

	// Show the new elemnts of the vector
	for(int x = 0; x < source.size(); x++)
	{
		std::cout << source[x] << " ";
	}
	std::cout << std::endl << std::endl;

	return 0;
}

Output:
int * var1 , char * var2

int var1* , char var2*

Press any key to continue

Title: lexer_merge_next_prepend Description: This function will search a vector to see if any element is equal to 'token'. If it is, then that token will be removed from the vector, but it will be prepended to the next element in the vector. If there is a 'space' passed in, the contents of that variable will seperate the token from the beginning of the string of the next element. URL: None Source:
void lexer_merge_next_prepend(std::vector<std::string>& source, const std::string& token, const std::string& space = "")
{
	std::vector<std::string>::iterator itr = std::find(source.begin(), source.end(), token);
	while(itr < (source.end() - 1))
	{
		std::string newStr = token;
		newStr += space;
		newStr += (*(itr + 1));
		(*(itr + 1)) = newStr;
		source.erase(itr);
		itr = std::find(source.begin(), source.end(), token);
	}
}

Example:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

void lexer_merge_next_prepend(std::vector<std::string>& source, const std::string& token, const std::string& space = "")
{
	std::vector<std::string>::iterator itr = std::find(source.begin(), source.end(), token);
	while(itr < (source.end() - 1))
	{
		std::string newStr = token;
		newStr += space;
		newStr += (*(itr + 1));
		(*(itr + 1)) = newStr;
		source.erase(itr);
		itr = std::find(source.begin(), source.end(), token);
	}
}

int main(int argc, char* argv[])
{
	std::vector<std::string> source;
	source.push_back("int");
	source.push_back("*");
	source.push_back("var1");
	source.push_back(",");
	source.push_back("char");
	source.push_back("*");
	source.push_back("var2");

	// Show the original elemnts of the vector
	for(int x = 0; x < source.size(); x++)
	{
		std::cout << source[x] << " ";
	}
	std::cout << std::endl << std::endl;

	// Merge all '*' at the end of the next element
	lexer_merge_next_prepend(source, "*");

	// Show the new elemnts of the vector
	for(int x = 0; x < source.size(); x++)
	{
		std::cout << source[x] << " ";
	}
	std::cout << std::endl << std::endl;

	return 0;
}

Output:
int * var1 , char * var2

int *var1 , char *var2

Press any key to continue

Title: RemoveAdjacentRepetitions Description: This function will use the unique algorithm to remove adjacent repetitions in a vector. URL: Original design Source:
template <class type>
void RemoveAdjacentRepetitions(std::vector<type> &vec)
{
	vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
}

Example:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

template <class type>
void RemoveAdjacentRepetitions(std::vector<type> &vec)
{
	vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
}

int main(int argc, char* argv[])
{
	std::vector<int> intList;
	for(int x = 0; x < 10; x++)
		intList.push_back(x);
	for(int x = 0; x < 10; x+=2)
		intList.push_back(x);

	// For this example, we will sort to repitions are next to each other
	sort(intList.begin(), intList.end());

	for(int x = 0; x <intList.size(); x++)
		std::cout << intList[x] << ' ';
	std::cout << std::endl << std::endl;

	// Remove any adjacent repeating elements
	RemoveAdjacentRepetitions(intList);

	for(int x = 0; x <intList.size(); x++)
		std::cout << intList[x] << ' ';
	std::cout << std::endl << std::endl;

	return 0;
}

Output:
0 0 1 2 2 3 4 4 5 6 6 7 8 8 9

0 1 2 3 4 5 6 7 8 9

Press any key to continue

Title: ToString Description: This function will take any data type that has the extraction operator defined and convert it to a string. It will work for all basics data types URL: None Source:
template <class type>
std::string ToString(type source)
{
	using namespace std;
	stringstream output;
	output << source;
	return output.str();
}

Example:
#include <iostream>
#include <string>
#include <sstream>

template <class type>
std::string ToString(type source)
{
	using namespace std;
	stringstream output;
	output << source;
	return output.str();
}

int main(int argc, char* argv[])
{
	std::string myStr1 = ToString(5);
	std::cout << myStr1 << std::endl;

	std::string myStr2 = ToString(3.1415);
	std::cout << myStr2 << std::endl;

	std::string myStr3 = ToString('a');
	std::cout << myStr3 << std::endl;
	return 0;
}

Output:
5
3.1415
a
Press any key to continue

Title: StringToUpper Description: This function will take a string and convert it to all upper case. URL: Original design Source:
void StringToUpper(std::string& source)
{
	std::transform(source.begin(), source.end(), source.begin(), toupper);
}

Example:
#include <iostream>
#include <string>
#include <algorithm>

void StringToUpper(std::string& source)
{
	std::transform(source.begin(), source.end(), source.begin(), toupper);
}

int main(int argc, char* argv[])
{
	using namespace std;
	string source = "ThIs Is A MiXeD CaSe StRiNg";
	cout << source << endl;
	StringToUpper(source);
	cout << source << endl;
	return 0;
}

Output:
ThIs Is A MiXeD CaSe StRiNg
THIS IS A MIXED CASE STRING
Press any key to continue

Title: StringToLower Description: This function will take a string and make it all lowercase URL: Original design Source:
void StringToLower(std::string& source)
{
	std::transform(source.begin(), source.end(), source.begin(), tolower);
}

Example:
#include <iostream>
#include <string>
#include <algorithm>

void StringToLower(std::string& source)
{
	std::transform(source.begin(), source.end(), source.begin(), tolower);
}

int main(int argc, char* argv[])
{
	using namespace std;
	string source = "ThIs Is A MiXeD CaSe StRiNg";
	cout << source << endl;
	StringToLower(source);
	cout << source << endl;
	return 0;
}

Output:
ThIs Is A MiXeD CaSe StRiNg
this is a mixed case string
Press any key to continue

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By Affgoo
      About us:
      We are a team of 14 developers developing multiple mid scope games both are over halfway complete. We polish all of our games and focus on quality.


      We are a small team, everyone currently on the team and future teammates must be interested in game development as a whole and not just one role, being a small indie company it is very important that you can wear a few hats and not just one. Everyone on our team is a game dev.

      looking for:
      3d Artist (hand painting a huge plus)
      3d Animator
      entry level Software engineer with reasonable skills in c# / shaders. 

      ^ requirement for all positions: A true love of game development and to be very self motivated.
      We are a very active team, you must be too. 


      If interested or for more information add me on skype: nicholas.boucher4


      Atlas Sentry art style: (art complete)(code complete)
      http://www.slidedb.com/games/atlas-sentry

      Rat n Gat art style: In Devlopment

    • By EvaBalikova
      Main menu in Feudal Alloy. 
      twitter
    • By Jcyshadow97
      Hi guys,i m looking for someone that can work with me on a "top-down" multiplayer fps as 2d and 3d artist.I used photon server and i can take the part of programming.For now i made only the basic gameplay of the game that include shooting,switch weapon and and damage player.If someone can help me please contact me via e mail: 270514974@libero.it.
      I really appreciate your collaboration and hope you have a good day.....
      Thanks for you time to read the post
      At the bottom i attach some screenshot of the current game,i m sorry that i can't attach a video...



    • By Raptor42
      I'm looking to form a new game development team, mostly for training purposes.
      About me:
      I'm a student - Unity C# developer, who worked part-time in this industry for a couple of years already. I've been a lead developer in many "random collab groups" as well as a few companies. I specialize in creating 2D games for Android, but I'm looking forward to trying out new things - especially 3D development.
      Currently, I've got one Android game close to a release so I'd work for this team in my spare time. 
      About the project:
      I've been thinking about creating a simple tycoon-like simulation game for Android (and PC eventually), inspired by the Game Dev Story (initially released by Kairosoft in 1997) https://en.wikipedia.org/wiki/Game_Dev_Story 
      I haven't done much planning though, therefore I'm looking forward to hearing out your ideas.
      Right now, I've only created a test 3D scene using placeholder models and implemented a simple pathfinding system for me to play around with:
      https://i.imgur.com/xAd0l4o.png
      https://i.imgur.com/nHZerOT.png
      I'm looking to work with people who are:
      - willing to take a position of a: 3D modeller/2D artist/Designer
      - not necessarily very experienced, but eager to learn and improve their skills
      - active - check in at least once a day
      If you'd like to apply for a different position which I didn't list here, you are welcome to contact me as well.
      While this project is created mostly for learning purposes, if we ever get to release it and generate any revenue - you will recieve a certain percentage of it.
       
      To Apply:
      Send an email to rk.softwaredev@gmail.com
      Introduce yourself and attach an example of your work (if you have any)
    • By EvaBalikova
      Hello.

      We're a two-member team and I'd like to introduce you our game - Feudal Alloy. 
      It's a metroidvania-style action RPG with fishbowl-powered medieval robots.

      We've been working on the project for a few months. I'd be happy for any feedback.
      teaser
      homepage
      twitter
      facebook
       
      Our Steam page is public now: 
      steam link
       
       



  • Popular Now