<p>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?</p>

<p>Possible non-solutions:</p>

<pre><code>vector&lt;string&gt; v;
v.push_back("abc");
b.push_back("xyz");

for(int i = 0; i &lt; v.size(); i++)
    cout &lt;&lt; v[i] &lt;&lt; endl;
</code></pre>

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

<p>Possible non-solution 2:</p>

<pre><code>string list[] = {"abc", "xyz"};
</code></pre>

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

<p>There must be an easy way of doing this.</p>

[{'answer_id': 35006, 'author': 'Will Dean', 'author_id': 987, 'author_profile': 'https://Stackoverflow.com/users/987', 'pm_score': 5, 'selected': False, 'text': '<blockquote>\n  <p>Problems - no way to get the number of strings automatically (that i know of).</p>\n</blockquote>\n\n<p>There is a bog-standard way of doing this, which lots of people (including MS) define macros like <code>arraysize</code> for:</p>\n\n<pre><code>#define arraysize(ar)  (sizeof(ar) / sizeof(ar[0]))\n</code></pre>\n'}, {'answer_id': 35182, 'author': 'Shadow2531', 'author_id': 1697, 'author_profile': 'https://Stackoverflow.com/users/1697', 'pm_score': 2, 'selected': False, 'text': '<p>Here\'s an example:</p>\n\n<pre><code>#include &lt;iostream&gt;\n#include &lt;string&gt;\n#include &lt;vector&gt;\n#include &lt;iterator&gt;\n\nint main() {\n    const char* const list[] = {"zip", "zam", "bam"};\n    const size_t len = sizeof(list) / sizeof(list[0]);\n\n    for (size_t i = 0; i &lt; len; ++i)\n        std::cout &lt;&lt; list[i] &lt;&lt; "\\n";\n\n    const std::vector&lt;string&gt; v(list, list + len);\n    std::copy(v.begin(), v.end(), std::ostream_iterator&lt;string&gt;(std::cout, "\\n"));\n}\n</code></pre>\n'}, {'answer_id': 35323, 'author': 'Tyler', 'author_id': 3561, 'author_profile': 'https://Stackoverflow.com/users/3561', 'pm_score': 5, 'selected': False, 'text': '<p>You can concisely initialize a <code>vector&lt;string&gt;</code> from a statically-created <code>char*</code> array:</p>\n\n<pre><code>char* strarray[] = {"hey", "sup", "dogg"};\nvector&lt;string&gt; strvector(strarray, strarray + 3);\n</code></pre>\n\n<p>This copies all the strings, by the way, so you use twice the memory.  You can use Will Dean\'s suggestion to replace the magic number 3 here with arraysize(str_array) -- although I remember there being some special case in which that particular version of arraysize might do Something Bad (sorry I can\'t remember the details immediately).  But it very often works correctly.</p>\n\n<p>Also, if you\'re really gung-ho about the one line thingy, you can define a variadic macro so that a single line such as <code>DEFINE_STR_VEC(strvector, "hi", "there", "everyone");</code> works.</p>\n'}, {'answer_id': 35356, 'author': 'Matthew Crumley', 'author_id': 2214, 'author_profile': 'https://Stackoverflow.com/users/2214', 'pm_score': 2, 'selected': False, 'text': '<blockquote>\n  <p>You can use Will Dean\'s suggestion [<code>#define arraysize(ar)  (sizeof(ar) / sizeof(ar[0]))</code>] to replace the magic number 3 here with arraysize(str_array) -- although I remember there being some special case in which that particular version of arraysize might do Something Bad (sorry I can\'t remember the details immediately). But it very often works correctly.</p>\n</blockquote>\n\n<p>The case where it doesn\'t work is when the "array" is really just a pointer, not an actual array. Also, because of the way arrays are passed to functions (converted to a pointer to the first element), it doesn\'t work across function calls even if the signature looks like an array &mdash; <code>some_function(string parameter[])</code> is really <code>some_function(string *parameter)</code>.</p>\n'}, {'answer_id': 35424, 'author': 'Anthony Cramp', 'author_id': 488, 'author_profile': 'https://Stackoverflow.com/users/488', 'pm_score': 7, 'selected': False, 'text': '<p>C++ 11 added initialization lists to allow the following syntax:</p>\n\n<pre><code>std::vector&lt;std::string&gt; v = {"Hello", "World"};\n</code></pre>\n\n<p>Support for this C++ 11 feature was added in at least <a href="http://gcc.gnu.org/projects/cxx0x.html" rel="noreferrer">GCC 4.4</a> and only in <a href="https://msdn.microsoft.com/en-us/library/hh567368.aspx#corelanguagetable" rel="noreferrer">Visual Studio 2013</a>.</p>\n'}, {'answer_id': 37330, 'author': 'DrPizza', 'author_id': 2131, 'author_profile': 'https://Stackoverflow.com/users/2131', 'pm_score': 2, 'selected': False, 'text': '<p>Instead of that macro, might I suggest this one:</p>\n\n<pre><code>template&lt;typename T, int N&gt;\ninline size_t array_size(T(&amp;)[N])\n{\n    return N;\n}\n\n#define ARRAY_SIZE(X)   (sizeof(array_size(X)) ? (sizeof(X) / sizeof((X)[0])) : -1)\n</code></pre>\n\n<p>1) We want to use a macro to make it a compile-time constant; the function call\'s result is not a compile-time constant.</p>\n\n<p>2) However, we don\'t want to use a macro because the macro could be accidentally used on a pointer.  The function can only be used on compile-time arrays.</p>\n\n<p>So, we use the defined-ness of the function to make the macro "safe"; if the function exists (i.e. it has non-zero size) then we use the macro as above.  If the function does not exist we return a bad value.</p>\n'}, {'answer_id': 47267, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<pre><code>#include &lt;boost/foreach.hpp&gt;\n\nconst char* list[] = {"abc", "xyz"};\nBOOST_FOREACH(const char* str, list)\n{\n    cout &lt;&lt; str &lt;&lt; endl;\n}\n</code></pre>\n'}, {'answer_id': 66180, 'author': 'Eclipse', 'author_id': 8701, 'author_profile': 'https://Stackoverflow.com/users/8701', 'pm_score': 3, 'selected': False, 'text': '<p>One possiblity is to use a NULL pointer as a flag value:</p>\n\n<pre><code>const char *list[] = {"dog", "cat", NULL};\nfor (char **iList = list; *iList != NULL; ++iList)\n{\n    cout &lt;&lt; *iList;\n}\n</code></pre>\n'}, {'answer_id': 124480, 'author': 'Ross Smith', 'author_id': 21426, 'author_profile': 'https://Stackoverflow.com/users/21426', 'pm_score': 2, 'selected': False, 'text': '<p>You can use the <code>begin</code> and <code>end</code> functions from the Boost range library to easily find the ends of a primitive array, and unlike the macro solution, this will give a compile error instead of broken behaviour if you accidentally apply it to a pointer.</p>\n\n<pre><code>const char* array[] = { "cat", "dog", "horse" };\nvector&lt;string&gt; vec(begin(array), end(array));\n</code></pre>\n'}, {'answer_id': 1893430, 'author': 'Dominic.wig', 'author_id': 184997, 'author_profile': 'https://Stackoverflow.com/users/184997', 'pm_score': 1, 'selected': False, 'text': '<pre><code>#include &lt;iostream&gt;\n#include &lt;string&gt;\n#include &lt;vector&gt;\n#include &lt;boost/assign/list_of.hpp&gt;\n\nint main()\n{\n    const std::vector&lt; std::string &gt; v = boost::assign::list_of( "abc" )( "xyz" );\n    std::copy(\n        v.begin(),\n        v.end(),\n        std::ostream_iterator&lt; std::string &gt;( std::cout, "\\n" ) );\n}\n</code></pre>\n'}, {'answer_id': 8820663, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': '<p>Declare an array of strings in C++ like this  : <code>char array_of_strings[][]</code></p>\n\n<p>For example : <code>char array_of_strings[200][8192];</code></p>\n\n<p>will hold 200 strings, each string having the size 8kb or 8192 bytes.</p>\n\n<p>use <code>strcpy(line[i],tempBuffer);</code>  to put data in the array of strings.</p>\n'}, {'answer_id': 35067543, 'author': 'kajol jain', 'author_id': 5853232, 'author_profile': 'https://Stackoverflow.com/users/5853232', 'pm_score': 1, 'selected': False, 'text': '<p>You can directly declare an array of strings like <code>string s[100];</code>.\nThen if you want to access specific elements, you can get it directly like <code>s[2][90]</code>. For iteration purposes, take the size of string using the\n<code>s[i].size()</code> function.</p>\n'}]
['https://Stackoverflow.com/questions/34987', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/779/']