Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何访问char数组中的字符串_C++_Oop - Fatal编程技术网

C++ 如何访问char数组中的字符串

C++ 如何访问char数组中的字符串,c++,oop,C++,Oop,在摩尔斯电码游戏中,我制作了一个字符数组,如下所示,我想把每个元素都作为数组[index];但这给了分割错误。谁能给出解决办法 const char * const array[3]={"apple","mango","banana"}; 您还没有发布显示segfault的代码,因此很难对可能导致它的原因进行完整的分析。但是,您访问的内容可能超出了存储单词的数组的边界,这会引发分段错误 至于解决方案,为什么不使用STL容器呢?沿着std::vector的思路进行的一些操作会很好: //Cont

在摩尔斯电码游戏中,我制作了一个字符数组,如下所示,我想把每个元素都作为数组[index];但这给了分割错误。谁能给出解决办法

const char * const array[3]={"apple","mango","banana"};

您还没有发布显示segfault的代码,因此很难对可能导致它的原因进行完整的分析。但是,您访问的内容可能超出了存储单词的数组的边界,这会引发分段错误

至于解决方案,为什么不使用STL容器呢?沿着
std::vector
的思路进行的一些操作会很好:

//Container that stores words
std::vector<std::string> MorseWords;

//To add a word
MorseWords.push_back("apple");

//To access a letter within a word
char letter = MorseWords[WordPosition][LetterPosition];
//存储单词的容器
std::向量MorseWords;
//加词
摩丝沃德。推回(“苹果”);
//访问一个单词中的字母
字符字母=MorseWords[WordPosition][LetterPosition];
以下示例使用上述原则:

std::vector<std::string> MorseWords;
MorseWords.push_back("apple");
MorseWords.push_back("banana");
MorseWords.push_back("carrot");

std::cout << MorseWords[1][0]; //Prints the 'b' of banana
std::矢量莫尔斯沃德;
摩丝沃德。推回(“苹果”);
把香蕉推回去;
把胡萝卜往后推;

不,没有人能在不知道问题是什么的情况下给出解决方案。没有人不看你的代码就知道问题出在哪里。你“获取每个元素”的代码是什么