Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.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+中的加速算法+;_C++_Algorithm_Optimization - Fatal编程技术网

C++ C+中的加速算法+;

C++ C+中的加速算法+;,c++,algorithm,optimization,C++,Algorithm,Optimization,TL;D:我的代码在爪哇很快,但在C++中是很慢的。为什么? #include <iostream> #include <vector> #include <map> #include <algorithm> using namespace std; int read(string data, int depth, int pos, vector<long>& wantedList) { // 91 = [

TL;D:我的代码在爪哇很快,但在C++中是很慢的。为什么?

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>


using namespace std;

int read(string data, int depth, int pos, vector<long>& wantedList) {
    // 91 = [
    if (data.at(pos) == 91) {
        pos++;
        // Get first part
        pos = read(data, depth + 1, pos, wantedList);
        // Get second part
        pos = read(data, depth + 1, pos, wantedList);
    } else {
        // Get the weight
        long weight = 0;
        while (data.length() > pos && isdigit(data.at(pos))) {
            weight = 10 * weight + data.at(pos++) - 48;
        }
        weight *= 2 << depth;
        wantedList.push_back(weight);
    }
    return ++pos;
}


int doStuff(string data) {
    typedef map<long, int> Map;
    vector<long> wantedList;
    Map map;
    read(data, 0, 0, wantedList);
    for (long i : wantedList) {
        if (map.find(i) != map.end()) {
            map[i] = map[i] + 1;
        } else {
            map[i] = 1;
        }
    }

    vector<int> list;
    for (Map::iterator it = map.begin(); it != map.end(); ++it) {
        list.push_back(it->second);
    }
    sort(list.begin(), list.begin() + list.size());
    cout << wantedList.size() - list.back() << "\n";
    return 0;

}

int main() {
    string data;
    int i;
    cin >> i;
    for (int j = 0; j < i ; ++j) {
        cin >> data;
        doStuff(data);
    }
    return 0;
}
#包括
#包括
#包括
#包括

对于那些想知道的人来说,这是一个关于算法的学校作业,但我不是在寻求帮助,因为我已经用Java完成了它。问题是,当我说到C++时,我的算法似乎很糟糕。p> 在java中,我得到的时间大约是0.6秒,而C++中,“同一”代码给出了2秒(超过了时间限制)。p>
有人愿意告诉我为什么会这样吗?我认为C++在处理此类问题时比java更快,

可能是复制的一个原因。

每当你在C++中通过值传递某个东西时,就会创建一个副本。对于像

double
int
或指针之类的东西,这不是问题

但是对于像
std::string
这样的对象,复制可能会很昂贵。由于不修改
数据
,因此通过常量引用传递数据是有意义的:

int read(const string &data, int depth, int pos, vector<long>& wantedList)
int读取(常量字符串和数据、int深度、int位置、向量和wantedList)

可能的原因之一是复制

每当你在C++中通过值传递某个东西时,就会创建一个副本。对于像

double
int
或指针之类的东西,这不是问题

但是对于像
std::string
这样的对象,复制可能会很昂贵。由于不修改
数据
,因此通过常量引用传递数据是有意义的:

int read(const string &data, int depth, int pos, vector<long>& wantedList)
int读取(常量字符串和数据、int深度、int位置、向量和wantedList)

这可能更适合该站点。也就是说,既然你是C++的新手,你是否打开编译器优化?java默认情况下是让它们运行的,但是C++通常需要启用它们。<代码>数据< /代码>有多大?您正在将其复制到所有位置。您可能希望通过引用传递字符串以进行查询。您根本不需要
list
。您正在对
map
中的所有值进行排序,然后取最大值。您只需使用
std::max_元素
map
中获取最大值即可。如果您不需要按键排序,也可以使用
std::unordered_map
,这可能更好地反映了您的Java代码。@nikolap哈哈,谢谢!这将时间从>2缩短到0.13这可能更适合该站点。也就是说,既然你是C++的新手,你是否打开编译器优化?java默认情况下是让它们运行的,但是C++通常需要启用它们。<代码>数据< /代码>有多大?您正在将其复制到所有位置。您可能希望通过引用传递字符串以进行查询。您根本不需要
list
。您正在对
map
中的所有值进行排序,然后取最大值。您只需使用
std::max_元素
map
中获取最大值即可。如果您不需要按键排序,也可以使用
std::unordered_map
,这可能更好地反映了您的Java代码。@nikolap哈哈,谢谢!这将时间从>2缩短到0.13