第二次迭代后指针不再工作 我经常学习挑战和练习C++。上个星期,我被指针卡住了
挑战是/曾经是创建一个包含类别的待办事项列表。我已经在中实现了todo列表部分,并从中开始工作 这是我为这项挑战准备的代码:第二次迭代后指针不再工作 我经常学习挑战和练习C++。上个星期,我被指针卡住了,c++,c++11,C++,C++11,挑战是/曾经是创建一个包含类别的待办事项列表。我已经在中实现了todo列表部分,并从中开始工作 这是我为这项挑战准备的代码: #include <string> #include <algorithm> #include <iostream> #include <vector> #include <iterator> class TodoItem; class Category{ public: Category(std:
#include <string>
#include <algorithm>
#include <iostream>
#include <vector>
#include <iterator>
class TodoItem;
class Category{
public:
Category(std::string name)
: name(name) {
}
void addToCategory(TodoItem* item){
items.push_back(item);
}
void removeFromCategory(const TodoItem* item){
auto it = std::find(items.begin(), items.end(), item);
if (it != items.end())
{
items.erase(it);
}
}
static Category combine(const Category &left, const Category &right){
Category combined(left.getName() + ", " + right.getName());
std::vector<TodoItem*> sortedLeft(left.items);
std::vector<TodoItem*> sortedRight(right.items);
std::sort(sortedLeft.begin(), sortedLeft.end());
std::sort(sortedRight.begin(), sortedRight.end());
std::set_intersection(sortedLeft.begin(), sortedLeft.end(), sortedRight.begin(), sortedRight.end(), std::back_inserter(combined.items));
return combined;
}
std::string getName() const{
return name;
}
bool operator==(const Category& a) const {
return a.getName() == getName();
}
bool operator!=(const Category& a) const {
return a != *this;
}
bool operator<(const Category& a) const {
return this->getName() < a.getName();
}
friend std::ostream& operator<<(std::ostream& os, const Category& category);
private:
std::string name;
std::vector<TodoItem*> items;
};
class TodoItem
{
public:
TodoItem(const std::string &item)
: task(item) {
}
void update(const std::string &item){
task = item;
}
std::string getTask() const{
return task;
}
bool operator==(const TodoItem& a) const {
return a.getTask() == getTask();
}
bool operator!=(const TodoItem& a) const {
return a != *this;
}
bool operator<(const TodoItem& a) const {
return this->getTask() < a.getTask();
}
friend std::ostream& operator<<(std::ostream& os, const TodoItem& item) {
os << item.getTask() << std::endl;
return os;
}
private:
std::string task;
};
inline std::ostream& operator<<(std::ostream& os, const Category& category) {
os << "----" << category.getName() << "----" << std::endl;
for (auto &item : category.items){
os << *item << std::endl;
}
return os;
}
class TodoList
{
public:
void add(const std::string &task, const std::string &category = "No category"){
Category* catPtr = nullptr;
for (size_t j = 0; j < categories.size(); j++)
{
if (categories[j] == Category(category))
{
catPtr = &categories[j];
break;
}
}
if (catPtr == nullptr)
{
categories.push_back(Category(category));
catPtr = &categories[categories.size() - 1];
}
for (size_t i = 0; i < items.size(); i++)
{
if (items[i] == TodoItem(task))
{
catPtr->addToCategory(&items[i]);
return;
}
}
items.emplace_back(TodoItem(task));
catPtr->addToCategory(&items[items.size() - 1]);
}
template<typename ... args>
void add(const std::string &task, const std::string &category, args... otherCategories){
add(task, category);
add(task, otherCategories...);
}
void update(const std::string &oldTask, const std::string &newTask){
auto itemIt = std::find(items.begin(), items.end(), TodoItem(oldTask));
if (itemIt != items.end())
{
itemIt->update(newTask);
}
}
template<typename ... args>
void viewList(std::ostream& os, args ... categoriesArgs) const{
os << getCategory(categoriesArgs...);
}
private:
Category getCategory(const std::string category) const{
return *std::find(categories.begin(), categories.end(), Category(category));
}
template<typename ... args>
Category getCategory(const std::string category, args ... categoriesArgs) const{
return Category::combine(*std::find(categories.begin(), categories.end(), Category(category)), getCategory(categoriesArgs...));
}
std::vector<TodoItem> items;
std::vector<Category> categories;
};
int main(){
TodoList list;
list.add("A pixel is not a pixel is not a pixel", "Programming");
list.add("The Scheme Programming Language", "Programming");
list.add("Memory in C", "Programming");
list.add("Haskell's School of Music", "Programming", "Music");
list.add("Algorithmic Symphonies from one line of code", "Programming", "Music");
list.add("Modes in Folk Music", "Music");
list.add("The use of the Melodic Minor Scale", "Music");
list.viewList(std::cout, "Music");
list.viewList(std::cout, "Programming");
list.update("Algorithmic Symphonies from one line of code", "Algorithmic Symphonies from one line of code in C");
list.viewList(std::cout, "Programming", "Music");
return 0;
}
有人能给我解释一下为什么会发生这种情况,以及我如何解决这个问题吗
PS:我使用的是visual studio,不认为这有什么关系。在Joachim Pileborg对
std::vector
的内部工作进行反馈后,我意识到我需要其他容器结构。对于结构std::forward_list
我没有以前遇到的问题。Astd::vector
如果没有足够的空间容纳新元素,可能会重新分配它的数据。此重新分配将使指向向量中元素的所有当前指针和迭代器无效。我确信在这个问题上有很多重复的地方。谢谢,这是否意味着我应该使用其他容器?这真的取决于你的用例。您还可以在将所有项添加到向量(并且您确定不会再添加更多项)后创建另一个循环,只需调用addToCategory
函数即可。感谢您的评论,我搜索了另一个基于链接列表的容器,并遇到了std::forward\u list
。这样做是可行的,因为指向对象的指针不会改变。因为你说你这样做是一种学习挑战,教一个人钓鱼等等。。。您应该尝试将此问题隔离到最小的可重复示例中。这将有几个副作用:1 /它将最小化问题的表面,并允许你更好地考虑替代方案,2 /它将帮助你更好地理解问题的细节,3 /识别问题经常导致快速解决,4/这将使在诸如SO这样的网站上进行交流变得更容易,并使受访者更容易通过一个具体的小例子引导您找到解决方案。
items.emplace_back(TodoItem(task));
catPtr->addToCategory(&items[items.size() - 1]);