【发布时间】:2019-10-09 19:25:45
【问题描述】:
我正在处理c++ 代码,我需要在列表的给定索引处插入一个项目。我有两个清单:
list <int> objectIdList; //This list will save id's of object
list <double> objectIdDtimeList; //This list will save time duration of each particular object id.
我有一个代码,我将所有对象 ID 保存在 objectIdList 中,如下所示:
Index Value
[0] 3
[1] 6
[2] 2
现在我需要将这些对象 ID (3, 6, 2) 的持续时间保存在 objectIdDtimeList 中。我的计划是将持续时间保存在索引处的列表中,即对象ID。例如,对于对象 id 3,其总持续时间将保存在索引 3 的列表中。对于 id 6 的对象,其持续时间将保存在索引 6 处。
为此,我计划使用list.insert(),如下所示:
time_t start, end;
time(&start);
/*
* SOME CODE HERE
* SOME CODE HERE
*/
for (auto id : objectIdList)
{
time(&end);
double time_passed = difftime(current, start); //Getting the time duration in seconds
list<int>::iterator it = objectIdList.begin();
advance(it, id); // iterator to point to object id index
objectIdDtimeList.insert(it, time_passed);
}
但上面的代码给出了以下错误:
Severity Code Description Project File Line Suppression State
Error (active) E0304 no instance of overloaded function "std::list<_Ty, _Alloc>::insert [with _Ty=double, _Alloc=std::allocator<double>]" matches the argument list
Severity Code Description Project File Line Suppression State
Error C2664 'std::_List_iterator<std::_List_val<std::_List_simple_types<_Ty>>> std::list<_Ty,std::allocator<_Ty>>::insert(std::_List_const_iterator<std::_List_val<std::_List_simple_types<_Ty>>>,unsigned __int64,const _Ty &)': cannot convert argument 1 from 'std::_List_iterator<std::_List_val<std::_List_simple_types<_Ty>>>' to 'std::_List_const_iterator<std::_List_val<std::_List_simple_types<_Ty>>>'
有什么方法可以实现这个功能。有没有其他选择可以做到这一点。谢谢
【问题讨论】:
标签: c++ visual-studio list insert