简体   繁体   English

C++简单字符串程序

[英]C++ simple string program

beginner here初学者在这里

i wrote the below in C++, it's a short program that currently takes 2 words as inputs, and outputs the same words back but the words are split into even and odd instead.我用 C++ 写了下面的内容,这是一个短程序,目前以 2 个单词作为输入,并输出相同的单词,但单词被分成偶数和奇数。 I would like to be able to do this for 'T' words instead, but I can't figure it out.我希望能够为“T”字做这件事,但我想不通。 I would like to be able to first input the number of words that will follow, for example 10. Then to input the words and get T results back.我希望能够首先输入将要跟随的单词数,例如 10。然后输入单词并返回 T 结果。 So instead of just 2 words, an unlimited amount with the user specifying.因此,用户指定的数量不限,而不仅仅是 2 个词。

I need to put the below into a function and go from there sometime, but I want to learn the best technique to do so - any advice please?我需要将下面的内容放入一个函数中并在某个时候从那里开始,但我想学习最好的技术来做到这一点 - 请问有什么建议吗?

Thanks!谢谢! Alex亚历克斯

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;


int main() {
    int T;
    cin >> T;

    string FirstWord;
    cin >> FirstWord;
    int LengthFirst;
    LengthFirst = FirstWord.length();        
    string EvenFirst;
    string OddFirst;
    for (int i = 0; i < LengthFirst; i += 2){
        EvenFirst = EvenFirst + FirstWord[i];
    }
    for (int i = 1; i < LengthFirst; i += 2){
        OddFirst = OddFirst + FirstWord[i];
    }
    string SecondWord;
    cin >> SecondWord;
    int LengthSecond;
    LengthSecond = SecondWord.length();
    string EvenSecond;
    string OddSecond;
    for (int i = 0; i < LengthSecond; i += 2){
        EvenSecond += SecondWord[i];
    }
    for (int i = 1; i < LengthSecond; i += 2){
        OddSecond += SecondWord[i];
    }

    cout << EvenFirst << " " << OddFirst << endl;
    cout << EvenSecond << " " << OddSecond << endl;

    return 0;
}

Think I got it here, I was over-thinking this one想我明白了,我想得太多了

I put it in a for loop, as below - so any number of words can be input, user has to input the number of test cases at the我把它放在一个 for 循环中,如下所示 - 所以可以输入任意数量的单词,用户必须在

 #include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> #include <string> using namespace std; int main() { int T; cin >> T; for (int i = 0; i < T; i++){ string FirstWord; cin >> FirstWord; int LengthFirst; LengthFirst = FirstWord.length(); string EvenFirst; string OddFirst; for (int i = 0; i < LengthFirst; i += 2){ EvenFirst = EvenFirst + FirstWord[i]; } for (int i = 1; i < LengthFirst; i += 2){ OddFirst = OddFirst + FirstWord[i]; } cout << EvenFirst << " " << OddFirst << endl; } return 0; }

Ultimately, you are performing the same task N times.最终,您将执行N次相同的任务。

First, let's discuss how to store the information.首先,让我们讨论如何存储信息。 Functionally, we have one string as input which yields two strings as output.从功能上讲,我们有一个字符串作为输入,它产生两个字符串作为输出。 std::pair (from <utility> ) lets us easily represent this. std::pair (来自<utility> )让我们可以轻松地表示这一点。 But for sake of even-odd, std::array might be a better representation for us.但是为了奇偶, std::array对我们来说可能是更好的表示。 Since we have a variable number of words as input, a variable number of std::array will be output.由于我们有可变数量的单词作为输入,因此将输出可变数量的std::array std::vector (from <vector> ) is our friend here. std::vector (来自<vector> )是我们的朋友。

Second, let's discuss how to process the information.其次,让我们讨论如何处理信息。 Using named variables for each output component does not scale, so let's switch to a fixed array (noted below as array<string,2> . By switching to a fixed array for output, addressing each split becomes a function of the loop index ( index % 2 ). Below is a solution that generalizes on a known split size at compile time.为每个输出组件使用命名变量不会缩放,所以让我们切换到一个固定数组(下面记为array<string,2> 。通过切换到一个固定数组进行输出,对每个拆分的寻址变成了循环索引( index % 2 ). 下面是一个在编译时对已知分割大小进行概括的解决方案。

#include <string>
#include <array>
#include <vector>
#include <iostream>

int main() {
  int N;
  std::cin >> N;

  constexpr const int Split = 2;
  using StringPack = std::array<std::string, Split>;
  std::vector<StringPack> output;
  for (int wordIndex = 0; wordIndex < N; ++wordIndex) {
    std::string word;
    std::cin >> word;
    StringPack out;
    {
      int index = 0;
      for (char c : word) {
        out[index % Split] += c;
        ++index;
      }
    }
    output.emplace_back(out);
  }
  for (const auto & out : output) {
    for (const auto & word : out) {
      std::cout << word << ' ';
    }
    std::cout << '\n';
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM