如何对 n 个字符串执行操作,然后在C++中打印它们

How to perform operations on n number of strings and than print them in C++?

本文关键字:C++ 然后 打印 操作 执行 字符串      更新时间:2023-10-16

我有一个程序,其中输入了许多 n 个C++字符串。我想用 for 循环输入它们,而不是在新行上打印函数 int operation(字符串 t(的结果?换句话说,我可以在输入字符串(针对每个字符串(后调用函数 operation(( 吗?

#include <iostream>
#include <string>
using namespace std;
int n,counter=0;
int operate (string t)
{
    int st=t.find_first_of('E');
    for(st; st<t.length(); st++)
    {
        if(t[st]=='.')
        {
            continue;
        }
        else
        {
            counter++;
        }
    }
    return counter;
}
string k;
int main()
{
    cin>>n;
    for (int i=1; i<=n; i++)
    {
        cin>>k;
        operate(k);
    }
    return 0;
}

它返回,但您没有存储返回值。

打印返回值,例如

cout<<operate(k);

否则将返回值分配给另一个变量。返回值存储在另一个变量中。

int temp;
temp = operate(k);
court<<temp;
相关文章: