如何将字符串向量中的每个单词更改为大写 [英] How to change each word in a string vector to upper case

查看:35
本文介绍了如何将字符串向量中的每个单词更改为大写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在询问有关读取单词序列并将值存储在向量中的问题.然后继续将向量中的每个单词更改为大写,并将关于 8 个单词的输出打印到一行.我认为我的代码要么很慢要么无限运行,因为我似乎无法实现输出.

I was inquiring about reading a sequence of words and storing the values in a vector. Then proceed to change each word in the vector to uppercase and print the out put with respect to eight word to a line. I think my code is either slow or running infinitely as i can't seem to achieve an output.

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
    string word;
    vector<string> text;
    while (getline(cin, word)) {
        text.push_back(word);
    }
    for (auto index = text.begin(); index != text.end(); ++index) {
        for ( auto it = word.begin(); it != word.end(); ++it)
            *it = toupper(*it);
        /*cout<< index << " " << endl;*/
    }

    for (decltype(text.size()) i = 0; i != 8; i++)
        cout << text[i] << endl;

    return 0;
}

推荐答案

至少据我所知,这里的想法是忽略现有的行结构,每行写出 8 个单词,无论换行符如何输入数据.假设这是正确的,我将首先从输入中读取单词,而不注意现有的换行符.

At least as far as I can tell, the idea here is to ignore the existing line structure, and write out 8 words per line, regardless of line breaks in the input data. Assuming that's correct, I'd start by just reading words from the input, paying no attention to the existing line breaks.

从那里开始,就是将单词大写,将它们写出来,并且(如果您是 8 的倍数,则换行.

From there, it's a matter of capitalizing the words, writing them out, and (if you're at a multiple of 8, a new-line.

我还会在大部分工作中使用标准算法,而不是编写自己的循环来执行解析,例如读取和写入数据.由于模式基本上只是读取一个单词,修改它,然后写出结果,它非常适合 std::transform 算法.

I would also use standard algorithms for most of the work, instead of writing my own loops to do the pars such as reading and writing the data. Since the pattern is basically just reading a word, modifying it, then writing out the result, it fits nicely with the std::transform algorithm.

执行此操作的代码可能如下所示:

Code to do that could look something like this:

#include <string>
#include <iostream>
#include <algorithm>

std::string to_upper(std::string in) {
    for (auto &ch : in)
        ch = toupper((unsigned char) ch);
    return in;
}

int main() {
    int count = 0;

    std::transform(
        std::istream_iterator<std::string>(std::cin),
        std::istream_iterator<std::string>(),
        std::ostream_iterator<std::string>(std::cout),
        [&](std::string const &in) {
            char sep = (++count % 8 == 0) ? '\n' : ' ';
            return to_upper(in) + sep;
        });
}

我们可以将每个字符串的大写实现为第二个 lambda,嵌套在第一个中,但 IMO 开始变得有点不可读.同样,我们可以使用 std::tranform 来实现 to_upper 内部的大写转换.

We could implement capitalizing each string as a second lambda, nested inside the first, but IMO, that starts to become a bit unreadable. Likewise, we could use std::tranform to implement the upper-case transformation inside of to_upper.

这篇关于如何将字符串向量中的每个单词更改为大写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆