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;i cout在我的系统中正常工作,没有问题。以下是示例代码:
#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;