如何申报C ++字符串数组? [英] How to declare an array of strings in C++?

查看:125
本文介绍了如何申报C ++字符串数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想遍历字符串中尽可能最好的方式静态数组中的所有元素。我希望能够宣布它在同一行,并轻松地添加/移除它的元素,而无需跟踪号码。听起来很简单,不是吗?

I am trying to iterate over all the elements of a static array of strings in the best possible way. I want to be able to declare it on one line and easily add/remove elements from it without having to keep track of the number. Sounds really simple, doesn't it?

可能不解决方案:

vector<string> v;
v.push_back("abc");
b.push_back("xyz");

for(int i = 0; i < v.size(); i++)
    cout << v[i] << endl;

问题 - 没办法用字符串列表

Problems - no way to create the vector on one line with a list of strings

可能无解2:

string list[] = {"abc", "xyz"};

问题 - 没有办法自动获得字符串的数量(据我所知)

Problems - no way to get the number of strings automatically (that I know of).

有必须这样做的一个简单的方法。

There must be an easy way of doing this.

推荐答案

借助提高好像是你在寻找什么分配库。它使集装箱分配常数比以往更容易。

The boost assign library seems to be exactly what you are looking for. It makes assigning constants to containers easier than ever.

这篇关于如何申报C ++字符串数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆