Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.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++ c++;:can';t访问常量字符串中的元素&;_C++ - Fatal编程技术网

C++ c++;:can';t访问常量字符串中的元素&;

C++ c++;:can';t访问常量字符串中的元素&;,c++,C++,word[0]返回整个字符串,而不是第一个元素 如何访问从函数获取的常量字符串和word中的元素 void something(const string& word) { int i = 0; for (int i = 0; i < word.size(); i++) cout << word[i]; } void某物(常量字符串和单词) { int i=0; for(int i=0;i

word[0]
返回整个字符串,而不是第一个元素

如何访问从函数获取的
常量字符串和word
中的元素

void something(const string& word)
{
    int i = 0;
    for (int i = 0; i < word.size(); i++)
     cout << word[i];
}
void某物(常量字符串和单词)
{
int i=0;
for(int i=0;icout在我的系统中正常工作,没有问题。以下是示例代码:

#include <iostream>
#include <string>

void myfunc(const std::string& word){

  std::cout << word[0] << std::endl;
}

int main(void){
   myfunc("test");

}

如果您只想将常量字符串转换为字符串,只需复制它

std::string new_word = word;
std::cout << new_word;
std::string new\u word=word;

std::难道这需要一些代码,以便我们可以看到单词[0]实际上指的是什么。完成后,它只是通过引用传递给函数的字符串。很抱歉,我的代码中出现了错误。这与读取cstring中的元素无关&
std::string new_word = word;
std::cout << new_word;