Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.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++类中有一个可选的赋值。任务基本上是这样的: Write a program that holds a string of at least 8 words. Do the following: 1. Replace the letters of first word with '?' 2. Turn the letters of the last word to uppercase_C++_String_For Loop_Vector - Fatal编程技术网

遍历存储在向量中的字符串的字符以替换字符C++; 所以我们在C++类中有一个可选的赋值。任务基本上是这样的: Write a program that holds a string of at least 8 words. Do the following: 1. Replace the letters of first word with '?' 2. Turn the letters of the last word to uppercase

遍历存储在向量中的字符串的字符以替换字符C++; 所以我们在C++类中有一个可选的赋值。任务基本上是这样的: Write a program that holds a string of at least 8 words. Do the following: 1. Replace the letters of first word with '?' 2. Turn the letters of the last word to uppercase,c++,string,for-loop,vector,C++,String,For Loop,Vector,我们班上还没有学习向量 当我第一次阅读作业时,将字符串存储到向量似乎是个好主意,所以我就这样做了 为了用“?”替换字符,我使用了for循环。我知道,如果我只需要将某些字符或每一个其他字符更改为“?”,这将不起作用 我的问题是如何将字符串的字符转换为大写。 我的思想过程是:for循环遍历最后一个单词中的所有字符,如果字符是小写的,它将转换为大写,如果它已经是大写的,它不会改变 我相信我的方法可以解决这个问题,我只是可能没有正确地表达自己,或者我在某个地方犯了一个愚蠢的错误。有人能帮我或把我推向正确

我们班上还没有学习向量

当我第一次阅读作业时,将字符串存储到向量似乎是个好主意,所以我就这样做了

为了用“?”替换字符,我使用了for循环。我知道,如果我只需要将某些字符或每一个其他字符更改为“?”,这将不起作用

我的问题是如何将字符串的字符转换为大写。 我的思想过程是:for循环遍历最后一个单词中的所有字符,如果字符是小写的,它将转换为大写,如果它已经是大写的,它不会改变

我相信我的方法可以解决这个问题,我只是可能没有正确地表达自己,或者我在某个地方犯了一个愚蠢的错误。有人能帮我或把我推向正确的方向吗? 还有哪些其他选项可以遍历存储在向量中的字符串的所有字符?有没有其他方法可以更好地解决这个问题?谢谢你抽出时间

#include <vector>
#include <cctype> 
#include <string>
#include <iostream>

using namespace std;

vector<string>words;

//stores words to vector words
void storeWords()
{
    cout << "Input 8 words: " << endl;
    string s = " ";
    for(int i=0; i<=7; i++)
    {
        cin >> s;
        words.push_back(s);
    }
}
//prints our words
void printWords()
{
    cout << "\n Words stored in vector: " << endl;
    for (const string s : words)
        cout << s << endl;
}

//replaces chars of the first word with a '?' sign
void replace1(vector<string>&v)
{
    cout << "\nReplaced characters of the first word " << words[0] << " with '?'" << endl;
    for (char c : words[0])
        cout << "?";
}

void replace2(vector<string>&v)
{
    for (char c : words[7])
    {
        if(islower(c))
            c = toupper(c);
    }
    cout << endl;
    cout << words[7]<<endl;
}


int main()
{
    storeWords();
    printWords();
    replace1(words);
    replace2(words);

    return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
矢量词;
//将单词存储到向量单词
void storeWords()
{
cout
c=toupper(c)
将指定
c
大写值。但是,更改
c
不会更改
单词[7]
中的内容。您可以通过直接引用字符(
char&c
)来绕过此问题

void replace2(向量&v)
{
for(char&c:words[7]){
c=toUpper(c);
}
不能

应该是:

for (char& c : words[7])
第一个版本修改局部变量,而第二个版本更改
单词[7]
中的实际字符

小符号(
&
)使
c
成为
单词[7]
中某个字符的引用,允许您像更改
单词[7][some_i]
一样更改
c

另外,我应该补充一点,您的
replace
函数不需要
vector
参数


为什么不从开始到结束对字符串进行适当的修改,直到第一个空格字符为止,只对字符串进行迭代呢?至于你的问题,在for循环中,你缺少了将
&
变量绑定到字符串中的实际值,而不是复制它。yr代码没有按照赋值说明执行。它说“编写一个包含至少8个单词的字符串的程序。”,它不会说“请求8个单词”。你需要读一行(getline)拼凑单词并识别第一个和最后一个单词替换1似乎是一个骗局,您没有修改存储的字符串。将单个单词传递到替换函数中,而不是整个向量,这使这些函数更具通用性这对我来说似乎是正确的-其他人在没有任何评论的情况下否决了投票哇。我认为这是另一个问题< <代码> > 循环,然后你改变它……以匹配我的答案…@ C650。自从我做C++以来,已经有一段时间了。我在编辑之后看到了你的答案。我看到评论中有人提到了这个修复。它明显比我以前创建本地副本的例子好,所以是的,我编辑了我的答案。仍然给了你1。
for (char c : words[7])
for (char& c : words[7])