我的指向 std::string 对象的指针列表无法正常工作,我不明白为什么

发布于 2024-12-07 08:25:24 字数 777 浏览 1 评论 0原文

你能帮我弄清楚这段代码吗: onCreate(xMsg) 由 WindowProcedure 在 WM_NCCREATE 消息上调用。问题出在迭代器解引用内。我似乎无法将迭代器设置为列表的 begin() ,然后获取迭代器的内容,它应该是指向可以与 MessageBox 中的 ->c_str() 一起使用的字符串的指针。

class MyController
{
public:
    MyController() { }
    ~MyController() { }
    void onCreate(xMessage *msg);

protected:
    std::list<std::string *> m_stringlist;
    std::list<std::string *>::iterator m_stringlistiter;
};

void onCreate(xMessage *msg)
{
    std::string *first_string = new std::string("Hello world");
    m_stringlist.push_back(first_string);
    // This is where my iterator comes into play and it doesn't seem to do the trick.

    m_stringlistiter = m_stringlist.begin();
    MessageBox(NULL, (*m_stringlistiter)->c_str(), "Title", MB_OK);
}

Can you help me figure out this code:
onCreate(xMsg) is called by the WindowProcedure on a WM_NCCREATE message. The problem is within the iterator dereferencing. I can't seem to set the iterator to the list's begin() and then get the contents of the iterator, which should be a pointer to a string which could be used with ->c_str() in a MessageBox.

class MyController
{
public:
    MyController() { }
    ~MyController() { }
    void onCreate(xMessage *msg);

protected:
    std::list<std::string *> m_stringlist;
    std::list<std::string *>::iterator m_stringlistiter;
};

void onCreate(xMessage *msg)
{
    std::string *first_string = new std::string("Hello world");
    m_stringlist.push_back(first_string);
    // This is where my iterator comes into play and it doesn't seem to do the trick.

    m_stringlistiter = m_stringlist.begin();
    MessageBox(NULL, (*m_stringlistiter)->c_str(), "Title", MB_OK);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

べ映画 2024-12-14 08:25:24

我们没有给出完整的示例,但我假设您在迭代器的创建和使用之间进行其他操作。

在对容器进行各种操作后,不保证迭代器有效。您不应该保存它们。

我敢打赌这会起作用吗?

class MyController
{
public:
    MyController() : title("Hello World") {}
    void onCreate(xMessage *msg);
    {
         MessageBox(NULL, title.c_str(), "Title", MB_OK);
    }
protected:
    std::string title;  // generic lists of strings with 
                        // magic indexes are hard to maintain.
};

这也更容易阅读和调试,其成本比软件开发的任何其他部分都要高得多。

大多数情况下,您应该将迭代器用作临时对象。它们很少扩展到函数/方法的范围之外。

struct some_struct {

  some_struct() {
    gui_text.push_back( std::string("Hello World!") );
  }
  void some_function() {
    std::list<std::string>::iterator iter = gui_text.begin();
    for (; iter != gui_text.end(); iter++) {
      std::cout << *iter << std::endl;
    }
  }

  std::list<std::string> gui_text;
};

从容器中添加、删除等操作将使迭代器失效。因此,只需在每个点重新创建迭代器即可。字符串和迭代器非常轻量级,因此您无需担心以这种方式进行优化。真的。

We aren't given the full example, but I assume that you are doing other manipulations in between the creation and use of the iterator.

Iterators are not guaranteed to be valid after varied operations on the container. You should not save them.

I'm willing to bet that this will work?

class MyController
{
public:
    MyController() : title("Hello World") {}
    void onCreate(xMessage *msg);
    {
         MessageBox(NULL, title.c_str(), "Title", MB_OK);
    }
protected:
    std::string title;  // generic lists of strings with 
                        // magic indexes are hard to maintain.
};

This is easier to read and debug too, which costs a lot more than any other part of software development.

You should use iterators as temporary objects, for the most part. Rarely do they extend outside of a function/method's scope.

struct some_struct {

  some_struct() {
    gui_text.push_back( std::string("Hello World!") );
  }
  void some_function() {
    std::list<std::string>::iterator iter = gui_text.begin();
    for (; iter != gui_text.end(); iter++) {
      std::cout << *iter << std::endl;
    }
  }

  std::list<std::string> gui_text;
};

Things like adding, deleting, etc from the container will invalidate iterators. So just recreate the iterator at each point. Strings and iterators are pretty lightweight, so you don't need to worry about optimizing in this way. Really.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文