【发布时间】:2015-02-03 11:34:24
【问题描述】:
在地图中,如果值本身是一个列表,那么我们如何显示它。
键:1 值:1,2,3(列表)
键:2 值:4,5,6(列表)
void somefunction()
{
cout << "Key: " << pos->first << endl;
cout << "Value:" << pos->second << endl;
}
但是如果pos->second包含一个List,如何显示呢?
我有一个存储所有目录及其对应文件的功能。
我在显示 VALUE 地图时遇到问题。我已经调试并观察到相应的值正在正确存储,但我无法显示它。由于 map 的 value 部分本身就是 pathiterator 的列表。每当我在不同的目录中获得相同的文件名时,我都会将引用或路径迭代器存储在对应于相同键(文件名)的映射中。
class FileMgr
{
public:
using path_1 = std::string;
using paths_1 = std::set<path_1>;
using pathiter = paths_1::iterator;
using listofiter = std::list<pathiter>;
using file = std::string;
using store = std::map<file, listofiter>;
using store_iter = store::iterator;
paths_1 pp;
pathiter oo;
listofiter l1;
store s6;
store_iter d;
void addPattern(const std::string& patt)
{
if (patterns_.size() == 1 && patterns_[0] == "*.*")
patterns_.pop_back();
patterns_.push_back(patt);
}
void search()
{
for (recursive_directory_iterator i("."), end; i != end; ++i)
{
pp.insert(i->path().parent_path());
oo = pp.find(i->path().parent_path());
if (!is_directory(i->path()))
{
s6[i->path().filename()].push_back(oo);
}
}
for ( d = s6.begin(); d != s6.end(); d++)
{
cout << "Key: " << d->first << " Value: ";
std::cout << "Value:";
for (auto& x : d->second)
std::cout << ' ' << x;
//^^Red Squiggly here ERROR
std::cout << '\n';
}
}
private:
std::string path_;
DataStore& store_;
patterns patterns_;
};
【问题讨论】:
-
您可以遍历列表并打印每个元素或重载运算符
<<fordecltype(pos->second),如果您在多个地方需要它。 -
您可以以与显示地图中未包含的列表完全相同的方式显示它。