迭代一对列表,列表在数组中
Iterating over list of pairs, the list being in an array
我已经搜索了宇宙最远的地方(也就是互联网),但没有找到一个关于如何解决我的问题的提示。所以我来找你。
我正在尝试迭代一个包含字符串对的列表。这个列表是数组中20个列表中的一个。这是我当前的代码:
logging.h:
#ifndef LOGGING_H
#define LOGGING_H
#include <iostream>
#include <list>
#include <string>
class logging
{
public:
void log(int,std::string,std::string);
void draw();
logging();
virtual ~logging();
private:
int displaylevel=0;
std::list<std::pair<std::string,std::string>> logd[20];
};
#endif // LOGGING_H
logging.cpp:
#include "logging.h"
#include <list>
#include <string>
#include <iostream>
logging::logging(){
//for future use
}
void logging::log(int level,std::string category, std::string entry) {
int thislevel;
for (thislevel=level-1;(thislevel>-1);thislevel--){
std::pair <std::string,std::string> newentry;
newentry = std::make_pair (category,entry);
logd[thislevel].push_front(newentry);
}
}
void logging::draw(){
//draw console on the screen using opengl
std::list<std::pair<std::string,std::string>>* log = &(logd[displaylevel]);
std::list<std::pair<std::string,std::string>>::iterator logit;
for ( logit = (*log).begin() ; logit != (*log).end() ; logit++ ) {
std::cout << (*logit).first() << std::endl << (*logit).second() << std::endl;
}
}
logging::~logging() {
//Deconstructor for log class (save log to file?)
}
这个想法是,如果一个重要事件5被记录,那么它将被放入列表0,1,2,3和4。通过简单地显示与该详细级别(由displaylevel定义)相对应的列表,可以在游戏中显示各种详细级别(如果控制台/日志打开)。然而,我似乎不能正确地迭代列表,它一直抛出一个不匹配调用std::basic_string错误。任何帮助都很感激,我对c++很陌生。
first
和second
是std::pair
的成员变量,而不是成员方法。去掉括号:
std::cout << (*logit).first << std::endl << (*logit).second << std::endl;
您不需要()
来访问std::pair
成员的.first
和.second
。它们是变量成员,不是方法。
删除它们:
std::cout << (*logit).first() << std::endl << (*logit).second() << std::endl;
^^ ^^
first &第二,它们不是成员函数。你不能像使用函数一样使用它们。去掉括号。此外,除了将logd设置为数组之外,还可以使用像这样的向量
std:: vector
还可以防止不必要的内存分配。
相关文章:
- C++如何生成std::数组列表
- 在数组中输入 n 个整数的列表,并以类似于钟摆来回移动的方式排列它们. 输入-1 3 2 5 4,输出5 3 1 2 4
- C++未知长度字符串的数组,其行为类似于 Python 字符串列表
- GTEST : 如何将数组列表作为参数传递到 ValuesIn of INSTANTIATE_TEST_CASE_P?
- 基于数组的列表 - 动态数组创建时出错
- 如何使用列表/数组在C++中实现 Python "... not in ..."?
- 从非托管 C# DLL 返回列表/数组
- C++对象列表数组
- 如何在C++中创建配对列表数组
- 链接列表数组
- 获取列表数组中的所有列表项
- 如何创建和扫描列表/数组..在嵌入式剪辑中
- 列表数组<string>,插入字符串行为奇怪
- 对 STL 列表数组使用迭代器的语法
- 在类|C++中创建分数列表数组
- C++是否有一些类似C#类型的东西来存储列表/数组中的类类型
- 列表数组-构造函数和main()中的地址不匹配
- c++中的列表数组.指针;/
- STL 列表数组无法访问列表函数
- 用动态内存管理创建类列表数组