我正在reddits dailyprogrammer定期挑战学习和练习C ++。上周challenge我被指针困住了。
挑战在于/是要创建一个包含类别的待办事项列表。我已经在previous challenge中实现了todo-list部分,我从那里开始。
这是我对此挑战的代码:
#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;
}
但是当我执行它时,我在方法TodoList::Add
中得到了错误的内存异常。
当我执行这些行时,指向前一个待办事项的指针变为invallid。
items.emplace_back(TodoItem(task));
catPtr->addToCategory(&items[items.size() - 1]);
有人可以向我解释为什么会发生这种情况以及如何解决这个问题吗?
PS:我正在使用视觉工作室,不要认为这很重要。答案 0 :(得分:2)
在对来自Joachim Pileborg的std::vector
内部工作的反馈之后,我意识到我需要其他容器结构。结构std::forward_list
我不会遇到以前遇到过的问题。