简体   繁体   English

将 Function 返回值存储到变量中

[英]Store Function Return Value into a Variable

I have these codes to find the size of an input, and extract the integer from the input:我有这些代码来查找输入的大小,并从输入中提取 integer:

void getInteger(string s) {

    stringstream str_strm;
    str_strm << s;
    string temp_str;
    int temp_int;
    while (!str_strm.eof()) {
        str_strm >> temp_str;
        if (stringstream(temp_str) >> temp_int) {
            cout << temp_int << "";
        }
        temp_str = "";
    }

}


int main()
{
    string myString;
    int totalChar;
    getline(cin, myString);
    int stringLength = myString.length();
    cout << stringLength << endl;;
    getInteger(myString);

}

How can I store the getInteger value into a variable so that I can compare/sum its value to the integer?如何将getInteger值存储到变量中,以便将其值与 integer 进行比较/求和? For example, the input is:例如,输入是:

I love 6我爱6

So the code should count the total characters and the integer of the input:所以代码应该计算输入的总字符数和 integer:

8 + 6 = 14 8 + 6 = 14

If you have a function that will extract an integer and include that integer in the output of a sum of the .length() and the integer, form all of your output in the function itself rather than complicating things and losing control of the initial input by doing part of the input in main() and part in your function. If you have a function that will extract an integer and include that integer in the output of a sum of the .length() and the integer, form all of your output in the function itself rather than complicating things and losing control of the initial input通过在main()中进行部分输入并在 function 中进行部分输入。

Approaching things in that manner you put complete control over your output in a single function.以这种方式处理事物,您可以在单个 function 中完全控制 output。 For example in your case, if there was no integer found how would you "un-output" the length?例如,在您的情况下,如果没有 integer 发现您将如何“取消输出”长度?

Putting those changes in place, your function could be similar to:将这些更改到位,您的 function 可能类似于:

void getint (std::string s)
{
    int val,                        /* var to hold int value */
        found = 0;                  /* flag indicating if int was found */
    size_t len = s.length();        /* get length of s */
    std::string tmp;                /* temporary string to read into from ss */
    std::stringstream ss (s);       /* create stringstream from string */

    while (ss >> tmp) {             /* read from ss */
        try {                       /* stoi must have exception handler */
            val = stoi (tmp);       /* attempt conversion */
            found = 1;              /* on success set found flag */
            break;                  /* break loop */
        }
        catch (const std::exception & e) {  /* catch exception get next word */
            continue;
        }
    }
    if (found)  /* if integer found, output your sum */
        std::cout << len << " + " << val << " = " << len + val << '\n';
    else        /* otherwise handle error */
        std::cerr << "error: no integer value in input.\n";
}

( note: the only error handling needed if no conversion takes place is to get the next word. Any output will just add lines between the input and your desired output) 注意:如果没有发生转换,唯一需要的错误处理是获取下一个单词。任何 output 只会在输入和所需输出之间添加行)

An example program utilizing the function could be:利用 function 的示例程序可以是:

#include <iostream>
#include <sstream>
#include <string>

void getint (std::string s)
{
    int val,                        /* var to hold int value */
        found = 0;                  /* flag indicating if int was found */
    size_t len = s.length();        /* get length of s */
    std::string tmp;                /* temporary string to read into from ss */
    std::stringstream ss (s);       /* create stringstream from string */

    while (ss >> tmp) {             /* read from ss */
        try {                       /* stoi must have exception handler */
            val = stoi (tmp);       /* attempt conversion */
            found = 1;              /* on success set found flag */
            break;                  /* break loop */
        }
        catch (const std::exception & e) {  /* catch exception get next word */
            continue;
        }
    }
    if (found)  /* if integer found, output your sum */
        std::cout << len << " + " << val << " = " << len + val << '\n';
    else        /* otherwise handle error */
        std::cerr << "error: no integer value in input.\n";
}

int main (void) {

    std::string s;

    if (getline (std::cin, s))
        getint (s);
    else
        std::cerr << "stream error or user canceled.\n";
}

Example Use/Output示例使用/输出

$ ./bin/lengthaddition
I love 6
8 + 6 = 14

with no integer in input输入中没有 integer

$ ./bin/lengthaddition
I love B
error: no integer value in input.

Look things over and let me know if you have further questions.如果您还有其他问题,请仔细查看并告诉我。

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

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