Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++通用交换函数_C++_Templates_Strtok - Fatal编程技术网

C++通用交换函数

C++通用交换函数,c++,templates,strtok,C++,Templates,Strtok,在我的课堂上,我得到了: template<class T> void Swap(T, T); 这看起来像是一个错误,因为它永远不会是真的: if(s == "") 这是将s的地址与字符串文本的地址进行比较:它不检查s是否为空字符串。使用strtok的替代方案是: 关于swap,正如在评论和其他答案中所述,只需使用std::swap。我猜lit是一个char[]so std::swaplit[I][k],lit[I][l];我会完全按照你的要求去做 编辑: 在给出声明字符串*li

在我的课堂上,我得到了:

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

这看起来像是一个错误,因为它永远不会是真的:

if(s == "")
这是将s的地址与字符串文本的地址进行比较:它不检查s是否为空字符串。使用strtok的替代方案是:

关于swap,正如在评论和其他答案中所述,只需使用std::swap。我猜lit是一个char[]so std::swaplit[I][k],lit[I][l];我会完全按照你的要求去做

编辑:

在给出声明字符串*lit且该lit看起来是示例用法中的数组的注释之后,以以下方式使用std::swap:

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"
1为什么Swap是类成员?类成员应该以某种方式与类紧密耦合。在大多数情况下,如果某个不使用私有成员或非常类似于使用私有成员的方法(即便利方法)的对象成为类成员,则这是糟糕设计的标志。C++不是java,所有的东西都必须属于一个类。使交换方法成为独立的模板方法

2更好的是,不要重新发明轮子。std::swap就在那里,它工作得非常好。在许多情况下,您可以期望标准库提供的方法和类比您编写的更好

3在你的课堂上,你调用了Sort方法,但问题是关于Swap的。因为你没有写下你期望发生的事情和实际发生的事情,这是我能找到的唯一可能是错误的事情


4在C++中不使用Strutk,除非你必须使用。char是C风格的字符串,不应该在C++中使用。使用STD::String代替.< /P>为什么不使用STD::SWAP而不是重新创建轮子?不要在C++中使用Strutk,请改用istream。class.cpp中的定义是Swap而不是Sort吗?还有一个问题,您可以发布Swap的定义和lit的声明吗?好的,但是对于4,是否有一种方法可以在std::string中的定界符之后拆分字符串?比如字符串myString=Something,delim='';字符串a[10]=myString.Splitdelim@摄魂怪:看吧。

int counter = 0;
    char* s = strtok(eq_clone ," ");
    while(s != NULL)
    {
        counter++;
        if(s == "")
            counter--;
        s = strtok(eq_clone ,"+-");
    }
if(s == "")
#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;
}
a
string
with
multiple
delimiters
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"
template<class T> void Swap(T&, T&);
                           //^ //^

Swap(lit[i][k], lit[i][l]);