This looks like a mistake as it will never be true:
if(s == "")
this is comparing the address of s
to the address of the string literal ""
: it is not checking if s
is an empty string. An alternative to using strtok()
would be boost::algorithm::split
:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>
int main()
{
std::string s("a,string+with-multiple delimiters");
std::vector<std::string> s_tokens;
boost::algorithm::split(s_tokens, s, boost::is_any_of(",+- "));
std::for_each(s_tokens.begin(),
s_tokens.end(),
[] (const std::string& s)
{
std::cout << s << "\n";
});
return 0;
}
Output:
a
string
with
multiple
delimiters
Regarding swap, as has already been stated in comments and other answer(s) just use std::swap()
. I am guessing that lit
is a char[][]
so std::swap(lit[i][k], lit[i][l]);
will do exactly what you require.
EDIT:
After comment giving declaration string* lit
and that lit
appears to be an array (from example usage) then use std::swap()
in this manner:
std::string* lit = new std::string[4];
lit[0] = "zero";
std::swap(lit[0][1], lit[0][2]); // The characters will be passed by reference,
// don't take address.
std::cout << lit[0] << "\n"; // Prints "zreo"
Note, that the declaration of your Swap()
passes arguments by value (incorrect), but the definition of Swap()
passes arguments by reference (correct). If you change the declaration of your Swap()
and invoke it as follows it will work (if you really don't want to use std::swap()
:
template<class T> void Swap(T&, T&);
//^ //^
Swap(lit[i][k], lit[i][l]);