[英]Find Iterator in a std::vector
我遇到问题,我正在尝试创建一个用于读写.ini文件的系统,但我一直坚持如何正确添加节/键。 我以为可以将它们作为对象添加到std :: vector中,所以我编写了以下代码段(删除了不相关的代码):
// .h
class Ini
{
public:
class IniSection
{
friend class Ini;
private:
IniSection(Ini *ini, std::string section);
~IniSection();
private:
Ini *pIni;
std::string sectionName;
};
vector<IniSection *>::iterator Ini::FindSection(std::string section);
IniSection *AddSection(std::string name);
private:
vector<IniSection *> Sections;
};
typedef Ini::IniSection IniSection;
// .cpp
IniSection::IniSection(Ini *ini, std::string section) : pIni(ini), sectionName(section)
{
}
vector<IniSection *>::iterator Ini::FindSection(std::string section)
{
IniSection tempSection(NULL, section);
return find(Sections.begin(), Sections.end(), tempSection); // ERROR
}
IniSection *Ini::AddSection(std::string name)
{
vector<IniSection *>::const_iterator iter = FindSection(name);
if (iter == Sections.end())
{
IniSection *newSection = new IniSection(this, name);
Sections.push_back(newSection);
return newSection;
}
else
return *iter;
}
如您所见,我用
//错误
这是因为当我尝试构建它时,出现此错误:
错误C2679二进制'==':未找到采用'const Ini :: IniSection'类型的右侧操作数的运算符(或没有可接受的转换)
它出现在算法库中。 所以我想我尝试获得迭代器的方式是错误的,所以也许有人可以帮助我。 我在互联网上找不到适合我的东西。 提前致谢!
您需要在您的类IniSection
添加一个operator==
以回答问题“ IniSection
2个实例IniSection
相等?”。 像这样:
bool operator==(const IniSection& lhs, const IniSection& rhs)
{
return (lhs.sectionName == rhs.sectionName);
}
上面的代码有几个问题。 如建议的那样,您需要为IniSection
定义一个operator ==
,以便std::find
能够比较Sections
向量中的条目。 但是,还有另一个问题: FindSection
有点用处,因为您无法测试迭代器是否在Sections::end()
处返回了点,因为Sections
是私有成员。 如果我建议,您需要添加类似hasSection
方法的方法,该方法返回bool
,并使用该方法测试是否存在节。 我随意修改代码来说明要点,可以根据需要随意更改。
#ifndef MAIN_HPP
#define MAIN_HPP
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
class Ini
{
public:
class IniSection
{
friend class Ini;
Ini& pIni;
std::string sectionName;
public:
IniSection(Ini& ini, const std::string& section);
friend bool operator == (const IniSection& _this, const IniSection& _other)
{
return _this.sectionName == _other.sectionName;
}
};
std::vector<IniSection>::iterator FindSection(const std::string& section);
std::vector<IniSection>::iterator AddSection(const std::string& section);
bool hasSection(const std::string& section);
std::vector<std::string> sectionNames();
private:
std::vector<IniSection> Sections;
};
typedef Ini::IniSection IniSection;
#endif // MAIN_HPP
#include "main.hpp"
IniSection::IniSection(Ini& ini, const std::string& section)
:
pIni(ini),
sectionName(section)
{}
std::vector<IniSection>::iterator Ini::FindSection(const std::string& section)
{
IniSection tempSection(*this, section);
return std::find(Sections.begin(), Sections.end(), tempSection);
}
std::vector<IniSection>::iterator Ini::AddSection(const std::string& section)
{
std::vector<IniSection>::iterator iter = FindSection(section);
if (iter == Sections.end())
{
Sections.emplace_back(*this, section);
return Sections.end() - 1;
}
else
{
return iter;
}
}
bool Ini::hasSection(const std::string& section)
{
return FindSection(section) != Sections.end();
}
std::vector<std::string> Ini::sectionNames()
{
std::vector<std::string> names;
for (const auto& s : Sections)
{
names.push_back(s.sectionName);
}
return names;
}
int main()
{
Ini ini;
ini.AddSection("Section1");
ini.AddSection("Section2");
ini.AddSection("Section3");
if (ini.hasSection("Section1"))
{
std::cout << "Ini contains section 1!\n";
}
if (!ini.hasSection("Section4"))
{
std::cout << "Adding section 4...\n";
ini.AddSection("Section4");
}
std::cout << "Ini contains the following sections:\n";
for (const auto& s : ini.sectionNames())
{
std::cout << "\t" << s << "\n";
}
return 0;
}
打印:
Ini contains section 1!
Adding section 4...
Ini contains the following sections:
Section1
Section2
Section3
Section4
另外,如注释中所建议, std::unordered_map
是出色的执行程序,非常适合此类情况。
PS:您可能已经注意到,我用对象替换了原始指针。 像std::vector
这样的STL容器在内部处理分配和释放,因此不需要使用原始指针。
编辑
我已经修改了pastebin中的代码,但是缺少了一些东西(我不确定IniPath
是什么类型-也许是std::string
吗?)。 有很多改进的余地,例如,您可以在getSection
中添加几行以自动添加一个节(如果该节不存在)(因此您不必首先使用hasSection
检查是否存在)。 如前所述,您可以从使用std::unordered_map
受益匪浅,因为插入,删除和查找均摊销了固定时间。 使用std::vector
,查找和检索元素将是线性时间。 我不清楚有关的另一件事就是为什么你需要存储的引用(或在原始代码的指针)的父对象(如IniSection&
内部IniKey
)。 据我所知,您没有使用这些。 只要您不存储原始指针,就不需要实现自己的析构函数。
免责声明:我尚未编译和测试此代码。
PS:您可能需要将此代码发布在CodeReview上以进行进一步的改进。
main.hpp
#include <vector>
#include <string>
#include <fstream>
#include <algorithm>
class Ini
{
public:
Ini();
~Ini();
bool Load(std::string path);
void Load(std::istream& input);
bool Save(std::string path);
void Save(std::ostream& output);
void Create(std::string path);
class IniSection
{
friend class Ini;
Ini& pIni;
std::string sectionName;
public:
IniSection(Ini& ini, const std::string& section);
~IniSection();
friend bool operator == (const IniSection& _this, const IniSection& _other)
{
return _this.sectionName == _other.sectionName;
}
std::string GetSectionName();
class IniKey
{
friend class IniSection;
IniSection& pSection;
std::string keyName;
std::string keyValue;
std::string commentValue;
public:
IniKey(IniSection& section, const std::string& key);
~IniKey();
friend bool operator == (const IniKey& _this, const IniKey& _other)
{
return _this.keyName == _other.keyName;
}
void SetValue(std::string value);
std::string GetValue();
std::string GetKeyName();
void AddComment(std::string comment);
std::string GetComment();
};
void RemoveAllKeys();
std::vector<IniKey>::iterator FindKey(const std::string& key);
std::vector<IniKey>::iterator AddKey(const std::string& key);
std::vector<IniKey>::iterator GetKey(const std::string& key);
bool hasKey(const std::string& key);
std::string GetKeyValue(const std::string& key);
void SetKeyValue(const std::string& key, const std::string& value);
private:
std::vector<IniKey> Keys;
};
void RemoveAllSections();
std::vector<IniSection>::iterator FindSection(const std::string& section);
std::vector<IniSection>::iterator AddSection(const std::string& section);
std::vector<IniSection>::iterator GetSection(const std::string& section);
bool hasSection(const std::string& section);
std::string GetKeyValue(std::string section, std::string key);
void SetKeyValue(std::string section, std::string key, std::string value);
private:
std::vector<IniSection> Sections;
};
typedef Ini::IniSection IniSection;
typedef IniSection::IniKey IniKey;
main.cpp
#include "main.hpp"
bool Ini::Load(std::string path)
{
std::ifstream input;
input.open(path.c_str(), std::ios::binary);
if (!input.is_open())
return false;
Load(input);
input.close();
return true;
}
void Ini::Load(std::istream& input)
{
std::vector<IniSection>::iterator section;
std::string lineValue;
enum
{
KEY,
SECTION,
COMMENT,
OTHER
};
while (getline(input, lineValue))
{
// TrimLeft(lineValue);
// TrimRight(lineValue, "\n\r");
if (!lineValue.empty())
{
unsigned int type = OTHER;
type = (lineValue.find_first_of("[") == 0 && (lineValue[lineValue.find_last_not_of(" \t\r\n")] == ']')) ? SECTION : OTHER;
type = ((type == OTHER) && (lineValue.find_first_of("=") != std::string::npos && lineValue.find_first_of("=") > 0)) ? KEY : type;
type = ((type == OTHER) && (lineValue.find_first_of("#") == 0)) ? COMMENT : type;
switch (type)
{
case SECTION:
section = AddSection(lineValue.substr(1, lineValue.size() - 2));
break;
case KEY:
{
size_t equalSpot = lineValue.find_first_of("=");
std::string keyName = lineValue.substr(0, equalSpot);
std::string keyValue = lineValue.substr(equalSpot + 1);
std::vector<IniKey>::iterator key = section->AddKey(keyName);
key->SetValue(keyValue);
break;
}
default:
break;
}
}
}
}
void Ini::Create(std::string path)
{
std::fstream file;
file.open(path, std::fstream::out);
file.close();
}
bool Ini::Save(std::string path)
{
std::ofstream output;
output.open(path.c_str(), std::ios::binary);
if (!output.is_open())
{
output.close();
return false;
}
Save(output);
output.close();
return true;
}
void Ini::Save(std::ostream& output)
{
std::string section;
std::vector<IniSection>::iterator iter1;
for (iter1 = Sections.begin(); iter1 != Sections.end(); iter1++)
{
section = "[" + iter1->GetSectionName() + "]";
output << section << "\r\n";
std::vector<IniKey>::iterator iter2;
for (iter2 = iter1->Keys.begin(); iter2 != iter1->Keys.end(); iter2++)
{
std::string comment = "# " + iter2->GetComment();
if (comment != "# ")
output << comment << "\r\n";
std::string key = iter2->GetKeyName() + "=" + iter2->GetValue();
output << key << "\r\n";
}
output << "\r\n";
}
}
std::string Ini::GetKeyValue(std::string section, std::string key)
{
if (hasSection(section))
{
auto s = GetSection(section);
if (s->hasKey(key))
{
return s->GetKey(key)->GetValue();
}
}
return std::string();
}
void Ini::SetKeyValue(std::string section, std::string key, std::string value)
{
if (hasSection(section))
{
auto s = GetSection(section);
if (s->hasKey(key))
{
s->GetKey(key)->SetValue(value);
}
}
}
// IniSection -----------------------------------------------------------------------------------
IniSection::IniSection(Ini& ini, const std::string& section) : pIni(ini), sectionName(section)
{
}
void Ini::RemoveAllSections()
{
// std::vector<IniSection *>::iterator iter;
// for (iter = Sections.begin(); iter != Sections.end(); iter++)
// delete *iter;
Sections.clear();
}
std::vector<IniSection>::iterator Ini::FindSection(const std::string& section)
{
IniSection tempSection(*this, section);
return std::find(Sections.begin(), Sections.end(), tempSection);
}
std::vector<IniSection>::iterator Ini::AddSection(const std::string& section)
{
std::vector<IniSection>::iterator iter = FindSection(section);
if (iter == Sections.end())
{
Sections.emplace_back(*this, section);
return Sections.end() - 1;
}
else
return iter;
}
std::vector<IniSection>::iterator Ini::GetSection(const std::string& section)
{
return FindSection(section);
}
std::string IniSection::GetSectionName()
{
return sectionName;
}
std::string IniSection::GetKeyValue(const std::string& key)
{
if (hasKey(key))
{
return GetKey(key)->GetValue();
}
return std::string();
}
void IniSection::SetKeyValue(const std::string& key, const std::string& value)
{
if (hasKey(key))
GetKey(key)->SetValue(value);
}
// IniKey -----------------------------------------------------------------------------------
void IniSection::RemoveAllKeys()
{
// std::vector<IniKey *>::iterator iter;
// for (iter = Keys.begin(); iter != Keys.end(); iter++)
// delete *iter;
// std::vector manages the allocations automatically
// as long as you are not using raw pointers
Keys.clear();
}
std::vector<IniKey>::iterator IniSection::FindKey(const std::string& key)
{
IniKey tempKey(*this, key);
return std::find(Keys.begin(), Keys.end(), tempKey);
}
std::vector<IniKey>::iterator IniSection::AddKey(const std::string& key)
{
if (hasKey(key))
{
return GetKey(key);
}
else
return Keys.insert(Keys.end(), {*this, key});
}
bool IniSection::hasKey(const std::string& key)
{
return FindKey(key) != Keys.end();
}
void IniKey::SetValue(std::string value)
{
keyValue = value;
}
std::string IniKey::GetValue()
{
return keyValue;
}
std::string IniKey::GetKeyName()
{
return keyName;
}
std::vector<IniKey>::iterator IniSection::GetKey(const std::string& key)
{
if (hasKey(key))
return GetKey(key);
return Keys.end();
}
bool Ini::hasSection(const std::string& section)
{
return FindSection(section) != Sections.end();
}
void IniKey::AddComment(std::string comment)
{
commentValue = comment;
}
std::string IniKey::GetComment()
{
return commentValue;
}
int main()
{
// How i want to use it:
// Write:
Ini ini;
ini.Create(IniPath);
ini.Load(IniPath);
// Check if "Test" section exists and add it if it doesn't
if (!ini.hasSection("Test"))
{
ini.AddSection("Test");
}
auto secTest(ini.GetSection("Test"));
secTest->AddKey("Key1")->SetValue("KeyValue1");
secTest->GetKey("Key1")->AddComment("This is a Test");
ini.Save(IniPath);
// Read:
Ini ini1;
ini1.Load(IniPath);
// IniSection *Section = ini.GetSection("Test");
if (ini1.hasSection("Test"))
{
if (ini1.GetSection("Test")->hasKey("Key1"))
{
std::string keyValue = ini.GetSection("Test")->GetKeyValue("Key1");
}
}
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.