如何"name"数组的元素?
How can I "name" the elements of an array?
所以我的问题是这样的:
给定我的数组
char *MyArray[] = {"string1","string2","string3","string4","string5","string6"};
如何命名 string1,以便当我尝试在数组中访问它时,我使用如下代码?
char *myString = MyArray[string1];
不幸的是,你不能做这样的事情;但是,你可以尝试以下方法,以便知道 string1 是什么,而不必返回到包含数组的标头:
enum ArrayNames : int
{
string1 = 0,
string2 = 1,
string3 = 2,
string4 = 3,
string5 = 4,
string6 = 5
};
char *myString = MyArray[ArrayNames.string1];
希望这可以帮助任何想要在使用数组时节省一些时间的人:)
如果你想按名称访问字符串,只需创建一些变量。
const string std::string
string1 = "string1",
string2 = "string2",
string3 = "string3",
string4 = "string4",
string5 = "string5";
您正在寻找不存在的问题的解决方案。
由于问题显然是关于如何访问元素const char*
数组的索引,
我相信下面的示例是使用枚举的简单直接的方法。
#include <iostream>
using namespace std;
int main()
{
enum StringCodes {
STRING1,
STRING2,
STRING3,
STRING4,
STRING5,
STRING6
};
const char *MyArray[] =
{"string1","string2","string3","string4","string5","string6"};
const char *string1 = MyArray[STRING1];
cout << string1;
return 0;
}
您可能希望使用另一个容器,例如 std::map
,它允许您将键与元素相关联:
map<string,const char*> myMap;
myMap["string1"] = "string1";
myMap["string2"] = "string2";
cout << myMap["string1"];
你可以通过使用 c++ 的 std::map 容器来实现这一点。地图需要两种数据类型。第一个是键,第二个是与键关联的值。键是用于获取关联值的内容。
std::map<std::string, std::string> map_of_strings;
/**
* Put your strings twice because the first is the key which
* enables you to get the value using the string. The second
* is the value. You can insert your key / values using any
* one of the following methods:
*
* Method 1:
* - Using std::pair which is the longer less pleasant
* looking way of adding to a map.
*/
map_of_strings.insert(std::pair<std::string, std::string>("string1", "string1"));
/**
* Method 2:
* - Using std::make_pair so you don't have to provide
* the two data types. Make pair will do it for you.
*/
map_of_strings.insert(std::make_pair("string2", "string2"));
/**
* Method 3:
* - Using emplace instead of insert. Emplace provides
* the same end result that insert does which is adding
* new pairs of data. However, emplace utilizies rvalues
* instead of creating a copy the given key / value. For
* more detailed information about the difference between
* insert and emplace, take a look at the stack article i've
* provided a link to below this code.
*/
map_of_strings.emplace(std::pair<std::string, std::string>("string3", "string3"));
/**
* Method 4:
* - Basically the same as method 3
*/
map_of_strings.emplace(std::make_pair("string4", "string4"));
/**
* Method 5:
* - Using square brackets like you would with an array
*/
map_of_strings["string5"] = "string5";
/**
* Method 6:
* - Using curly braces to declare single or multiple key / value pairs.
*/
map_of_strings = {{"string6", "string6"}, {"string7", "string7"}};
// Get string1 from the map - Tada!
std::string str = map_of_strings["string1"];
插入、置位和 [] 之间的区别
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 使用strcpy将char数组的元素复制到另一个数组
- 如何将元素添加到数组的线程安全函数?
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- 输出没有重复元素的动态数组(收缩数组)C++
- 数组中最大的非重复元素
- 数组元素打印的递归方法
- 对字符数组中的元素执行逐位操作
- 缓存std::数组的选定元素,并在c++中自动保持其一致性
- 如何计算数组中元素的位数?(不是数组的长度),并计算其数字的总和
- 通过交换元素使数组相同
- 按平均值替换数组中的元素
- 打印矢量数组中的所有元素
- 如何将字节数组元素替换为修改的十六进制 ASCII 符号?
- 如何为 c++ 的不同变量类型的结构元素创建动态数组?
- 如何将元素从向量转移到新数组?
- 如何在向量数组中插入元素?
- 为什么 2 个相同数组的元素彼此相等
- C++ 中的二维整数数组,每行中的元素数量不均匀