How to: literal string arrays...

Hi everyone,

I'm creating a program for which I want to create some basic, templated "starting" sequences. These are only run once and don't need to be fast, so readability is key. So far I've used a C-array, but I'd really like to use strings. So far I have this:

1
2
// header.h
const char* template_arr[];


1
2
// source.cpp
const char* template_arr[] = {"string 1","string 2","string 3","etc..."};


Naturally, I'm trying to learn c++, not c, so I really would prefer to use string arrays (I need to iterate through these later so that brings obvious issues too), but is it possible to create string arrays this neatly in c++?

To illustrate what I mean by "neat", here is an example of the actual string array:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
namespace Edhen
{
	namespace GeneTemplate
	{
		const char* simple_ribosome[] = {
			// first step is to go to a negative memory index and set the memory value to something positive
			"IDXDEC", // go to mem index -1
			"CLEAR","DROP","INC" // set memory value to 1

			// next loop through every subsequent value (the input base sequence) forever (until output limit is reached)
			"WHILEPOS", // while memory > 0
				
				// move forward and output new memory pointer
				"IDXINC", // increment memory pointer
				"OUT", // return memory value
				
				// now destroy the memory pointer and set it to something positive so that the loop continues
				"TEN","DROP" // set the memory value to 10 (a positive number, doesn't matter which)
			"END", // repeat the loop
			"EOA", // declare end of array (for interpreter only - not included in base sequence)
		};
	}
}


The string arrays are actually "templates" for commands that should be written in a manner readable (including the indents) by a software engineer. My worry is that if I use something like strvec.push_back("string") at every line it's going to ruin the readability.

Is there any way to make string vectors in c++ as pretty and light-weight as in C?

Please note that in this instance performance is not a concern; these arrays are converted into function pointers and manipulated from there, so it's really just to provide a visually meaningful representation to the user.
I do not understand why are you going to make your life harder? If the array will not be changed and its elements have qualifier const it is better to deal with the array instead of std::string.

And you can iterate through this array using standard C++ functions sttd::begin and std::end.

By the way if I am not mistaken (everybody can check this himself) every object of type std::string occupies 16 bytes in MS VC++ do not taking into account the memory allocated dynamically for storing string literals.
Last edited on
> Is there any way to make string vectors in c++ as pretty as in C?

They can be as pretty as they are in C
 
extern const std::vector<std::string> command_strings ;


1
2
3
4
5
6
const std::vector<std::string> command_strings
{
    "IDXINC", // go to mem index -1
    "CLEAR", "DROP", "INC" // set memory value to 1  //
    /* , ... */
}; 




> Is there any way to make string vectors in c++ as light-weight as in C?

There is no equivalent of std::string or std::vector<> in the C library. And if you create equivalent functionality in C, it would not be any lighter.


Having said that, if the strings and the size of the array containing them are known at compile time, there is no requirement that std::string or std::vector<> must be used. This is perfectly fine C++:

1
2
3
4
5
namespace constants
{
    extern const char* const command_strings[] ;
    extern const std::size_t num_commands ;
}


1
2
3
4
5
6
7
8
9
10
namespace constants
{
    const char* const command_strings[] =
    {
        "IDXINC", // go to mem index -1
        "CLEAR", "DROP", "INC" // set memory value to 1  //
        /* , ... */
    };
    const std::size_t num_commands = sizeof(command_strings) / sizeof( *command_strings ) ;
}
Thanks guys, that really cleared things up :)
Topic archived. No new replies allowed.