在列表C 中插入对象
Insert object in list c++
我有这个类:
class UserLogRecord
{
public:
std::string timestamp;
std::string id;
std::string name;
std::string data;
};
我正在迭代一个具有我的logdata的字符串向量,对于每个迭代,我都保存到我的列表中。
std::vector<std::string> lines = explode(filedata,'n');
std::list<UserLogRecord* > userLogRecords;
UserLogRecord* userLogRecord = new UserLogRecord;
//vector
for(int i = 0; i < lines.size(); i++)
{
std::vector<std::string> elements = explode(lines[i], ';');
userLogRecord->timestamp = elements.at(0);
userLogRecord->id = elements.at(5);
userLogRecord->name = elements.at(6);
userLogRecord->data = elements.at(3);
userLogRecords.push_back(userLogRecord);
}
Explode
函数是解析FIFEDATA的辅助函数。我的FIFEDATA只有两个日志,因此for
循环运行两次。之后,如果我遍及我的列表并显示时间戳和ID,它显示了2次,但相同的日志(最后一个具有相同时间戳,相同ID的日志(。
我是C 的新手,但是怎么了?我需要更改我的UserLogRecord
以包括构造函数,并在循环中添加元素:
UserLogRecord* userLogRecord = new UserLogRecord(elements.at(0), elements.at(5), elements.at(6), elements.at(3));
然后使用push_back
将userLogRecord
添加到列表中?还是我的第一个实施应该起作用,但是我做错了什么?
您的第一个方法应该起作用;想一想,您可能需要发布预期/实际输出。如果您自己编码explode
,也许很难进行调试,也许会发布该代码,并且可能是您问题的原因。
相关文章:
- 将包含不可复制对象的对插入到映射中
- 在 std::map 中插入对象时构造函数中变量的地址
- 在 c++ 列表中插入对象
- 在列表C 中插入对象
- 如何创建插入对象使用duktape
- 如何在地图中插入对象以保持其排序
- bad_alloc通过右值和对象有效性插入对象时
- 插入对象以设置c++
- 在地图中插入对象而不复制对象
- C++:使用递归在二叉搜索树中插入对象
- 如何在矢量中高效、正确地插入对象
- vector插入对象的副本或引用
- 我可以插入对象到tr1 unordered_map
- 在ostreams中插入对象
- 如何在指针数组中插入对象
- 在链表中按升序插入对象
- 标记分派结构不能插入对象类型
- 链表-插入对象
- 在vector的下标处插入对象
- 按字母顺序向vector插入对象