带数组的列表(向量)定义

发布于 2024-09-02 11:30:38 字数 553 浏览 3 评论 0原文

我有班级电子邮件, 她的构造函数中有参数“bcc”。 它实际上是副本电子邮件列表。 这些电子邮件的数量没有固定数量,以后我必须有可能扩展此列表。

//constructor prototype
Email::Email(vector<string> bcc)

所以我想使用向量或列表类型以及函数push_back()。 如何使用密件抄送电子邮件创建新实例?

我实际上需要声明和列表的定义。

我发现这个定义带有整数类型的迭代器:

int myints[] = {16,2,77,29};
Email myEmail(vector<int> (myints, myints + sizeof(myints) / sizeof(int) ));

,但它不是非常用户友好,我需要它与字符串。

有这样的事吗?

Email myEmail(vector<string> ("first","second","third"));

I have Class Email,
there is parameter "bcc" in her constructor.
Its actually list of emails for copies.
There is no fixed number of these emails and later i have to have possibility to extend this list.

//constructor prototype
Email::Email(vector<string> bcc)

So i want to use type vector or list for that and function push_back().
How can i make a new instance with bcc emails?

I need actually declaration with definition for my list.

I've found this definition with iterator for integer type:

int myints[] = {16,2,77,29};
Email myEmail(vector<int> (myints, myints + sizeof(myints) / sizeof(int) ));

, but its not very user friend and i need it with strings.

Is there something like this?

Email myEmail(vector<string> ("first","second","third"));

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

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

发布评论

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

评论(3

甜妞爱困 2024-09-09 11:30:38

除了 C++0x 列表初始化之外,还有 Boost.Assign 库应该做类似的事情。

Apart from C++0x list-initialization, there is the Boost.Assign library which should do similar things.

夜血缘 2024-09-09 11:30:38

如果你有C++0x,你可以做向量{“第一”,“第二”,“第三”}。否则,您将必须在某个范围内创建一个新向量,并手动推送您想要的每个向量,然后构建。

另外,您确实应该参考该向量,它确实很大。

您应该使用 std::vector ,除非您知道需要将项目插入到中间而不是末尾。

If you have C++0x, you can do vector { "first", "second", "third" }. Else, you will have to create a new vector in scope somewhere and manually push on each that you want, then construct.

Also, you should really take that vector by reference, it's really quite large.

You should use a std::vector unless you know that you will need to insert items into the middle, not on the end.

笨笨の傻瓜 2024-09-09 11:30:38

如果您不使用C++0x,那么您无权访问初始化程序。您能否添加一个采用任何旧迭代器的构造函数,

#include <vector>
#include <list>
#include <iostream>

struct Email
{
    typedef std::vector<std::string> BccType;
    BccType m_bcc;

    template <typename T>
    Email(const T& iter, const T& end)
        :m_bcc(iter, end)
    {
    }

    // Purely here for illustrative purposes...
    void display()
    {
        std::cerr << m_bcc.size() << " addresses..." << std::endl;
        for (BccType::iterator iter = m_bcc.begin(), iterEnd = m_bcc.end(); iter != iterEnd; ++iter)
        {
            std::cerr << *iter << std::endl;
        }
    }
};

int main(int, char*[])
{
    // Plain old char* array...
    const char* bcc[] = {"Jeff", "Joel", "Larry", "Brin"};
    const size_t count = sizeof bcc / sizeof bcc[0];
    Email email(&bcc[0], bcc + count);
    email.display();

    // STL container variation...
    std::list<std::string> names;
    names.push_back("Bill");
    names.push_back("Steve");
    Email reply(names.begin(), names.end());
    reply.display();
    return 0;
}

当然,您没有理由不能拥有一个采用 const BccType& 的 ctor ; (为了简洁和可维护性而进行typedef编辑)。请注意,我建议通过引用传递此值,以节省复制 std::vector 两次。

If you're not using C++0x then you have no access to initialisers. Could you add a constructor that takes any old iterator, viz:

#include <vector>
#include <list>
#include <iostream>

struct Email
{
    typedef std::vector<std::string> BccType;
    BccType m_bcc;

    template <typename T>
    Email(const T& iter, const T& end)
        :m_bcc(iter, end)
    {
    }

    // Purely here for illustrative purposes...
    void display()
    {
        std::cerr << m_bcc.size() << " addresses..." << std::endl;
        for (BccType::iterator iter = m_bcc.begin(), iterEnd = m_bcc.end(); iter != iterEnd; ++iter)
        {
            std::cerr << *iter << std::endl;
        }
    }
};

int main(int, char*[])
{
    // Plain old char* array...
    const char* bcc[] = {"Jeff", "Joel", "Larry", "Brin"};
    const size_t count = sizeof bcc / sizeof bcc[0];
    Email email(&bcc[0], bcc + count);
    email.display();

    // STL container variation...
    std::list<std::string> names;
    names.push_back("Bill");
    names.push_back("Steve");
    Email reply(names.begin(), names.end());
    reply.display();
    return 0;
}

There is, of course, no reason why you can't have a ctor that takes const BccType& (typedefed for brevity and maintainability) additionally. Note that I suggest passing this by reference to save copying the std::vector twice.

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