-3

In my class.h i got:

template<class T> void Swap(T, T);

And in class.cpp:

template<class T>
void class::Swap(T& p1, T& p2)
    {
        T aux = p1;
        p1 = p2;
        p2 = aux;
    }

When I try :

this->Swap<char*>(&lit[i][k], &lit[i][l]);

And another question: What am i doing wrong here: i want to split my string after some delimitators ("+-") and strtok isn't working as I expected.

int counter = 0;
    char* s = strtok(eq_clone ," ");
    while(s != NULL)
    {
        counter++;
        if(s == "")
            counter--;
        s = strtok(eq_clone ,"+-");
    }
Dementor
  • 241
  • 1
  • 6
  • 12

2 Answers2

3

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]);
hmjd
  • 120,187
  • 20
  • 207
  • 252
1

1) Why is Swap() a class member? Class members should somehow be tightly coupled to your classes. In most cases it is is sign of bad design if something, which does not use private members or is very similar to a method that does (i.e. a convinience method), becomes a class member. C++ is not java where everything has to belong to a class. Make your swap() method a free standing template method.

2) Better yet, do not reinvent the wheel. std::swap() is there and it works mightily well. In many cases you can expect the methods and classes provided by the standard library to work better than something you could write up.

3) In your class you called the method Sort(), but the question is about Swap(). Since you did not write what you expected to happen and what actually happens, this is the only thing I can find which might be wrong.

4) Do not use strtok() in C++ unless you have to. char* are C-Style strings and should not be used in C++. Use std::string instead.

LiKao
  • 10,408
  • 6
  • 53
  • 91
  • ok but for 4) is there a method to split the string after a delimitator in std::string? Something like string myString = "some thing", delim = ' '; string a[10] = myString.Split(delim); ? – Dementor Mar 10 '12 at 13:05
  • @Dementor: see [How do I tokenize a string in C++?](http://stackoverflow.com/q/53849/113124) and [How to split a string in C++?](http://stackoverflow.com/q/236129/113124). – Lazer Mar 10 '12 at 13:15