Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.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++的新手,我在试题平台上尝试教育练习,但是在这个平台上我应该使用不超过64 MB的内存。我的代码使用超过130 MB #include <sstream> #include <string> #include <fstream> #include <iterator> #include <vector> #include <map> using namespace std; template<class Container> void splitString(const std::string &basicString, Container &cont, char delim = ' ') { std::stringstream ss(basicString); std::string token; while (std::getline(ss, token, delim)) { cont.push_back(token); } } int main() { int target = 0; int count = 0; std::map<int, int> set; string line; ifstream fileR("input.txt"); std::vector<string> c; if (fileR.is_open()) { while (getline(fileR, line)) { if (count == 0) { target = std::stoi(line); count++; continue; } splitString(line, c); for (auto &d : c) { int key = std::stoi(d); if (set.count(key)) { set[key] += 1; } else { set[key] = 1; } } c.clear(); } fileR.clear(); fileR.close(); } ofstream fileW; fileW.open("output.txt"); bool found = false; for (const auto &p : set) { int d = target - p.first; if (set.count(d)) { if (p.first != d || set[d] > 1) { fileW << 1; found = true; break; } } } if (!found) { fileW << 0; } fileW.close(); return 0; } #包括 #包括 #包括 #包括 #包括 #包括 使用名称空间std; 模板 void splitString(const std::string和basicString,Container和cont,char delim=''){ 标准::stringstream ss(基本串); 字符串标记; while(std::getline(ss、token、delim)){ 继续推回(令牌); } } int main(){ int目标=0; 整数计数=0; std::映射集; 弦线; ifstream文件管理器(“input.txt”); std::向量c; if(fileR.is_open()){ while(getline(文件管理器,行)){ 如果(计数=0){ 目标=标准::stoi(线); 计数++; 继续; } 拆分字符串(第行,c); 适用于(自动和d:c){ int key=std::stoi(d); if(设置计数(键)){ 设置[键]+=1; }否则{ 设置[键]=1; } } c、 清除(); } fileR.clear(); fileR.close(); } 流式锉刀; 文件打开(“output.txt”); bool-found=false; 用于(常数自动和p:设置){ int d=目标-p.优先; 如果(设置计数(d)){ 如果(p.first!=d | | set[d]>1){ fileW_C++_Algorithm_Memory - Fatal编程技术网 1){ fileW,c++,algorithm,memory,C++,Algorithm,Memory" /> 1){ fileW,c++,algorithm,memory,C++,Algorithm,Memory" />

有没有办法减少我的c++;密码? 我是C++的新手,我在试题平台上尝试教育练习,但是在这个平台上我应该使用不超过64 MB的内存。我的代码使用超过130 MB #include <sstream> #include <string> #include <fstream> #include <iterator> #include <vector> #include <map> using namespace std; template<class Container> void splitString(const std::string &basicString, Container &cont, char delim = ' ') { std::stringstream ss(basicString); std::string token; while (std::getline(ss, token, delim)) { cont.push_back(token); } } int main() { int target = 0; int count = 0; std::map<int, int> set; string line; ifstream fileR("input.txt"); std::vector<string> c; if (fileR.is_open()) { while (getline(fileR, line)) { if (count == 0) { target = std::stoi(line); count++; continue; } splitString(line, c); for (auto &d : c) { int key = std::stoi(d); if (set.count(key)) { set[key] += 1; } else { set[key] = 1; } } c.clear(); } fileR.clear(); fileR.close(); } ofstream fileW; fileW.open("output.txt"); bool found = false; for (const auto &p : set) { int d = target - p.first; if (set.count(d)) { if (p.first != d || set[d] > 1) { fileW << 1; found = true; break; } } } if (!found) { fileW << 0; } fileW.close(); return 0; } #包括 #包括 #包括 #包括 #包括 #包括 使用名称空间std; 模板 void splitString(const std::string和basicString,Container和cont,char delim=''){ 标准::stringstream ss(基本串); 字符串标记; while(std::getline(ss、token、delim)){ 继续推回(令牌); } } int main(){ int目标=0; 整数计数=0; std::映射集; 弦线; ifstream文件管理器(“input.txt”); std::向量c; if(fileR.is_open()){ while(getline(文件管理器,行)){ 如果(计数=0){ 目标=标准::stoi(线); 计数++; 继续; } 拆分字符串(第行,c); 适用于(自动和d:c){ int key=std::stoi(d); if(设置计数(键)){ 设置[键]+=1; }否则{ 设置[键]=1; } } c、 清除(); } fileR.clear(); fileR.close(); } 流式锉刀; 文件打开(“output.txt”); bool-found=false; 用于(常数自动和p:设置){ int d=目标-p.优先; 如果(设置计数(d)){ 如果(p.first!=d | | set[d]>1){ fileW

有没有办法减少我的c++;密码? 我是C++的新手,我在试题平台上尝试教育练习,但是在这个平台上我应该使用不超过64 MB的内存。我的代码使用超过130 MB #include <sstream> #include <string> #include <fstream> #include <iterator> #include <vector> #include <map> using namespace std; template<class Container> void splitString(const std::string &basicString, Container &cont, char delim = ' ') { std::stringstream ss(basicString); std::string token; while (std::getline(ss, token, delim)) { cont.push_back(token); } } int main() { int target = 0; int count = 0; std::map<int, int> set; string line; ifstream fileR("input.txt"); std::vector<string> c; if (fileR.is_open()) { while (getline(fileR, line)) { if (count == 0) { target = std::stoi(line); count++; continue; } splitString(line, c); for (auto &d : c) { int key = std::stoi(d); if (set.count(key)) { set[key] += 1; } else { set[key] = 1; } } c.clear(); } fileR.clear(); fileR.close(); } ofstream fileW; fileW.open("output.txt"); bool found = false; for (const auto &p : set) { int d = target - p.first; if (set.count(d)) { if (p.first != d || set[d] > 1) { fileW << 1; found = true; break; } } } if (!found) { fileW << 0; } fileW.close(); return 0; } #包括 #包括 #包括 #包括 #包括 #包括 使用名称空间std; 模板 void splitString(const std::string和basicString,Container和cont,char delim=''){ 标准::stringstream ss(基本串); 字符串标记; while(std::getline(ss、token、delim)){ 继续推回(令牌); } } int main(){ int目标=0; 整数计数=0; std::映射集; 弦线; ifstream文件管理器(“input.txt”); std::向量c; if(fileR.is_open()){ while(getline(文件管理器,行)){ 如果(计数=0){ 目标=标准::stoi(线); 计数++; 继续; } 拆分字符串(第行,c); 适用于(自动和d:c){ int key=std::stoi(d); if(设置计数(键)){ 设置[键]+=1; }否则{ 设置[键]=1; } } c、 清除(); } fileR.clear(); fileR.close(); } 流式锉刀; 文件打开(“output.txt”); bool-found=false; 用于(常数自动和p:设置){ int d=目标-p.优先; 如果(设置计数(d)){ 如果(p.first!=d | | set[d]>1){ fileW,c++,algorithm,memory,C++,Algorithm,Memory,你的向量(c)在循环外声明,并且不会在每次调用split string时清除。这意味着每次传入要拆分的字符串时,向量都包含上一次运行的内容。这是有意为之的吗?如果不是,则在调用split string之前将向量移到循环中,或者在split string函数中清除它。如果是有意为之AL请提供更多关于你的代码应该做什么的信息。这是来自于一些竞赛/挑战/竞争性编码/黑客网站。是吗?如果你的目标是学习C++,你将无法学到任何东西。在几乎所有的情况下,像这一个,正确的解决方案是基于数学或编程技巧。如果你不

你的向量(c)在循环外声明,并且不会在每次调用split string时清除。这意味着每次传入要拆分的字符串时,向量都包含上一次运行的内容。这是有意为之的吗?如果不是,则在调用split string之前将向量移到循环中,或者在split string函数中清除它。如果是有意为之AL请提供更多关于你的代码应该做什么的信息。

这是来自于一些竞赛/挑战/竞争性编码/黑客网站。是吗?如果你的目标是学习C++,你将无法学到任何东西。在几乎所有的情况下,像这一个,正确的解决方案是基于数学或编程技巧。如果你不知道W。这个技巧是,尝试编写一种蛮力的方法,程序运行缓慢,失败了。如果你想学习C++,你就不能从毫无意义的网络竞赛网站中学习任何东西。要小心标签。很多监控C标签的人也都精通C++,但是如果他们想回答C++问题,他们就会是Muni。对C++标签进行拖动。我们不能真正帮助你,而不知道这个代码试图解决的问题。关于这行,<代码>如果(P.Suth.D= D [S] [D]>1),你知道这是什么行为吗?