V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
V2EX  ›  wevsty  ›  全部回复第 38 页 / 共 74 页
回复总数  1463
1 ... 34  35  36  37  38  39  40  41  42  43 ... 74  
2017 年 10 月 25 日
回复了 scenix 创建的主题 C C++的 set 爆内存,求助
#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;

const string IN_FNAME = "/home/xubuntu/v2ex/test/test_py.txt";
unordered_map<string, unordered_set<string> > dict;

//字符串分割
std::vector<string> string_split(const string &str_data,
const string &str_separator
, string::size_type n_max_split_count = (string::size_type) - 1)
{
std::vector<string> vec_split_str;
string str_subtmp;
string::size_type n_datalen = str_data.length();
string::size_type n_separator = str_separator.length();
string::size_type n_start = 0;
string::size_type n_index = 0;
if (n_max_split_count == 0)
{
return vec_split_str;
}
do
{
if (n_max_split_count == 0)
{
n_max_split_count--;
break;
}
n_index = str_data.find(str_separator, n_start);
if (n_index != string::npos)
{
str_subtmp = str_data.substr(n_start, n_index - n_start);
if (str_subtmp.length() != 0)
{
vec_split_str.push_back(str_subtmp);
}
n_start = n_index + n_separator;
if (n_start >= n_datalen)
{
break;
}
}
else
{
str_subtmp = str_data.substr(n_start);
if (str_subtmp.length() != 0)
{
vec_split_str.push_back(str_subtmp);
}
break;
}
} while (n_index != string::npos);
return std::move(vec_split_str);
}

void read_message(const string& filepath)
{
fstream rin(filepath);
string buf;
string sep(" ");
std::vector<string> vec_list;
vec_list.reserve(20);
while (getline(rin, buf)) {
vec_list = string_split(buf, sep);
for (size_t i = 1; i < vec_list.size(); i++)
{
dict[vec_list[0]].insert(vec_list[i]);
}
vec_list.clear();
}
}

int main()
{
clock_t start = clock();
read_message(IN_FNAME);
clock_t end = clock();
cout << "time: " << ((double)end - (double)start) / CLOCKS_PER_SEC << " sec" << '\n';
cout << "Total Keys:" << dict.size() << '\n';
return 0;
}
2017 年 10 月 25 日
回复了 scenix 创建的主题 C C++的 set 爆内存,求助
stringstream 的实现好像确实不是很快,所以我改了一下。
修改以后在 xubuntu 下面跑了一下,测试的数据是用图里 python 里写的 write_test_file 这个函数生成的。
[img]https://i.loli.net/2017/10/25/59f03fd257b2c.png[/img]
在 O2 优化下程序的时间是 0.267 秒,O3 优化的话和 O2 差不太多,python 下面则是 0.477 秒。
下一楼贴代码
2017 年 10 月 25 日
回复了 scenix 创建的主题 C C++的 set 爆内存,求助
@scenix 速度的问题,上测试用例。对于测试用例 @williamscorn 的代码也未必就是速度最优的选择,剩下的具体问题具体分析。
2017 年 10 月 25 日
回复了 scenix 创建的主题 C C++的 set 爆内存,求助
@williamscorn 按照我的理解,从楼主的意图来说,也许不应该用 std::set,而是用 std::list 或者 std::vector 更合适一些。
2017 年 10 月 25 日
回复了 scenix 创建的主题 C C++的 set 爆内存,求助
这个 cpp 写的。。。
line.substr(start,end)这里的问题前面已经有人指出来了,end 不应该是结束为止的标号,而是复制的长度。
同理 end = line.size() - 1;一样是有问题的。
unordered_map<string, unordered_set<string>* > big_dict;
这个定义是一个 key 指向一个不会重复的 string 指针,big_dict[key]->insert(&value);实际是插入了 value 这个 string 的指针,然而 value 在循环结束的时候就因为生存周期结束而销毁了,所以你才觉得这样不会爆内存。

不要用那么多指针来掩饰,你程序里想表达的数据结构实际上就是:
unordered_map<string,unordered_set<string> >
在 map 里套 set,那么 key 至少存在 2 次,存在重复查找,重复存放一堆问题,效率能高才怪了
比如日志中某一行是
“ key value1 value2 ”那么运行完成以后数据结构实际上是
{'key':{'key':'value2'}}
而你的 python 代码对应的结果应该是
{'key':['value1','value2']}
从结果上看,不要谈效率,代码的实现完全都是不对的。
2017 年 10 月 25 日
回复了 HunterMz 创建的主题 问与答 求助大陆和香港账户的转账问题
最好是楼主自己去开个香港银行卡,最好是和朋友同一家银行,这样朋友转给你就免费了。
如果都用工银亚洲的话,国内工行 atm 取款免费。
2017 年 10 月 24 日
回复了 loser 创建的主题 阅读 求推荐给父母的科普书籍
中医是一个不可以证伪的东西,靠科普很难根除。
不建立科学思维,大概是很难理解这些的,与其期待改变,不如随他们去吧。
2017 年 10 月 23 日
回复了 F2Sky 创建的主题 问与答 飞利浦的电动牙刷是不是没有原装的牙刷头了?
官方自营还不放心啥,和原装不一样就不一样呗,有些小改款也是很正常的。
2017 年 10 月 23 日
回复了 dekira 创建的主题 C C++可以用变量定义数组了么
@nullcoder
不用太纠结字面的说法。
变长数组通常用来指代的是能直接用变量来定义数组的这种特性。
malloc 或者 new 出来的一个空间也可以当成数组用,这样也确实实现了变量来定义数组这种事实。
一定要纠结的话,可以理解为变长数组是用变量定义数组长度的一个子集。
2017 年 10 月 23 日
回复了 dekira 创建的主题 C C++可以用变量定义数组了么
对 C 来说,C99 允许定义变长数组,然而有些编译器一直不肯支持(没错,说的就是你 MSVC )。
对 CPP 来说,变长数组这个特性标准上就没有支持,而且更 cpp 的做法是使用 vector 这样的 STL 容器。
2017 年 10 月 21 日
回复了 longislet 创建的主题 Windows 两年前的 win10 要更新吗?
这种大版本升级最好直接重装,直接升容易有问题。
2017 年 10 月 20 日
回复了 ballshapesdsd 创建的主题 问与答 c++内存泄露在程序退出之后还存在吗?
会的,这就是操作系统干的活
都是内核崩溃,不知道那个地址上有什么东西或者是哪个驱动导致的。
一般来说,我会首先排除安装在这个机器上的各种带驱动的软件,比如各种安全产品,防火墙(系统自带的除外)用 Autoruns 之类的检查一下看看有哪些驱动。

虽然我认为概率比较小,不过也可以尝试排除一下硬件问题,跑个内存测试也没坏处。
2017 年 10 月 10 日
回复了 justou 创建的主题 C C++如何获取本地编码的名称
Windows 下面默认的 locale 就算 C,而不是系统的代码页
你可以用
std::locale::global(std::locale("chs"));
来设置 locale。
如果你想知道 Windows 系统的代码页设定,那么需要调用 API。
比如:GetACP,GetCPInfoEx
给你个例子
#include <windows.h>
void cout_loc()
{
CPINFOEX cpinfo = { 0 };
GetCPInfoEx(CP_ACP, 0, &cpinfo);
std::cout << cpinfo.CodePageName << std::endl;
}
2017 年 10 月 9 日
回复了 blankme 创建的主题 问与答 世特力 Century 发货空包裹,好气啊
Orico 一生黑
1 ... 34  35  36  37  38  39  40  41  42  43 ... 74  
About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3199 Online   Highest 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 48ms · UTC 12:43 · PVG 20:43 · LAX 05:43 · JFK 08:43
♥ Do have faith in what you're doing.