How to declare an array of strings in C ++?

I am trying to iterate over all the elements of a static array of strings in the best way. I want to be able to declare it on one line and easily add / remove elements from it without tracking the number. It sounds very simple, right?

Possible solutions:

vector<string> v; v.push_back("abc"); b.push_back("xyz"); for(int i = 0; i < v.size(); i++) cout << v[i] << endl; 

Unable to create vector on single line with list of lines

Possible non-solution 2:

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

Problems - there is no way to automatically get the number of rows (what I know).

There should be an easy way to do this.

+75
c ++ arrays
Aug 29 '08 at 18:41
source share
15 answers

Boosting library assignment seems to be exactly what you are looking for. It makes assigning constants to containers easier than ever.

+26
Aug 29 '08 at 18:44
source share

C ++ 11 added initialization lists to allow the following syntax:

 std::vector<std::string> v = {"Hello", "World"}; 

Support for this feature C ++ 11 was added at least GCC 4.4 and only in Visual Studio 2013 .

+88
Aug 29 '08 at 22:39
source share

You can briefly initialize vector<string> from a statically created char* array:

 char* strarray[] = {"hey", "sup", "dogg"}; vector<string> strvector(strarray, strarray + 3); 

This copies all lines, by the way, so you use memory twice. You can use Will Dean's suggestion to replace the magic number 3 here with arraysize (str_array) - although I remember there is some special case where this particular version of arraysize can do something bad (sorry, I can’t remember the details right away) But it very often works correctly.

Also, if you really play on one line, you can define a variable macro so that one line works, such as DEFINE_STR_VEC(strvector, "hi", "there", "everyone"); .

+34
Aug 29 '08 at 21:21
source share

Problems - there is no way to automatically get the number of rows (what I know).

There is a standard way to execute bots that many people (including MS) define arraysize macros for:

 #define arraysize(ar) (sizeof(ar) / sizeof(ar[0])) 
+23
Aug 29 '08 at 18:48
source share

Declare an array of strings in C ++ as follows: char array_of_strings[][]

For example: char array_of_strings[200][8192];

will contain 200 lines, each line of which has a size of 8kb or 8192 bytes.

use strcpy(line[i],tempBuffer); to place data in an array of strings.

+9
Jan 11 2018-12-12T00:
source share

One possibility is to use the NULL pointer as the flag value:

 const char *list[] = {"dog", "cat", NULL}; for (char **iList = list; *iList != NULL; ++iList) { cout << *iList; } 
+7
Sep 15 '08 at 19:48
source share

You can use the begin and end functions from the Boost range library to easily find the ends of a primitive array, and unlike macros, this will result in a compilation error instead of breaking behavior if you accidentally use this pointer.

 const char* array[] = { "cat", "dog", "horse" }; vector<string> vec(begin(array), end(array)); 
+4
Sep 23 '08 at 23:05
source share

You can use the Will Dean clause [ #define arraysize(ar) (sizeof(ar) / sizeof(ar[0])) ] to replace the magic number 3 here with arraysize (str_array) - although I remember there is some kind a special case where this particular version of arraysize can do something bad (sorry, I can't remember the data right away). But it very often works correctly.

The case when it does not work is when the "array" is just a pointer, not the actual array. In addition, due to the way arrays are passed into functions (converted to a pointer to the first element), it does not work through function calls, even if the signature looks like an array - some_function(string parameter[]) really some_function(string *parameter) .

+3
Aug 29 '08 at 21:34
source share

Tried to flip Craig H to answer that you should use boost :: assign, but I don't have rep: (

I came across a similar technique in the first article I ever read by Andrei Alexandrescu in the C / C ++ Users Journal, Volume 16, No. 9, September 1998, pp. 73-74 (there is a full quote, because she is in the comments my implementation of his code, which I have used since).

Templates are your friend.

+3
Sep 15 '08 at 22:06
source share

Here is an example:

 #include <iostream> #include <string> #include <vector> #include <iterator> int main() { const char* const list[] = {"zip", "zam", "bam"}; const size_t len = sizeof(list) / sizeof(list[0]); for (size_t i = 0; i < len; ++i) std::cout << list[i] << "\n"; const std::vector<string> v(list, list + len); std::copy(v.begin(), v.end(), std::ostream_iterator<string>(std::cout, "\n")); } 
+2
Aug 29 '08 at 20:01
source share

Instead of this macro, I can suggest the following:

 template<typename T, int N> inline size_t array_size(T(&)[N]) { return N; } #define ARRAY_SIZE(X) (sizeof(array_size(X)) ? (sizeof(X) / sizeof((X)[0])) : -1) 

1) We want to use a macro to make it a compile-time constant; the result of the function call is not a compile-time constant.

2) However, we do not want to use a macro, because a macro can be accidentally used on a pointer. The function can only be used for compile-time arrays.

So, we use a specific function to make the macro "safe"; if the function exists (i.e., has a nonzero size), then we use the macro as described above. If the function does not exist, we return a bad value.

+2
Sep 01 '08 at 1:05
source share
 #include <boost/foreach.hpp> const char* list[] = {"abc", "xyz"}; BOOST_FOREACH(const char* str, list) { cout << str << endl; } 
+2
Sep 06 '08 at 5:34
source share
 #include <iostream> #include <string> #include <vector> #include <boost/assign/list_of.hpp> int main() { const std::vector< std::string > v = boost::assign::list_of( "abc" )( "xyz" ); std::copy( v.begin(), v.end(), std::ostream_iterator< std::string >( std::cout, "\n" ) ); } 
+1
Dec 12 '09 at 13:33
source share
 #include <iostream.h> #include <iomanip.h> int main() { int n; cout<<"enter the maximum number\n"; cin>>n; cout<<"enter the first number\n"; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { cin>>a[i][j]; } } cout<<"enter the second number\n"; for(int i=0;i<n;i++) { for(int k=0;k<n;k++) { cin>>b[i][k]; } } cout<<"the product will be\n"; for(int i=0;i<n;i++) { for(int g=0;g<n;g++) { c[i][g]=c[i][c]*c[i][j]; cout<<setw(5)<<c[i][g]; } cout<<endl; } return 0; } 
+1
Apr 08 2018-11-11T00:
source share

You can directly declare an array of strings, for example string s[100]; . Then, if you want to access certain elements, you can get it directly as s[2][90] . For iteration purposes, take the row size using s[i].size() .

+1
Jan 28 '16 at 17:03
source share



All Articles