我如何在c ++中使用std :: transform?

时间:2016-04-21 23:31:09

标签: c++ iterator stdstring stl-algorithm

我只是学习c ++,并创建了一个简单的计算器程序。与此相关,我有几个问题:首先,我能做些什么来清理它,或者做得更好?其次,在这段代码中,我使用goto语句返回程序的开头。我听说很多程序员都忽视了goto,因为它可以创建" spaghetti"码。我有什么办法可以不使用goto,而是使用某种类型的llop?最后,我从互联网上搜索了一行代码:

std::transform(returnToMain.begin(), returnToMain.end(), returnToMain.begin(), ::toupper);

有人可以向我解释这是做什么的吗?我理解transform

::toupper

但不是.begin().end()

这是我的完整代码,谢谢你的帮助:

#include <iostream>
#include <stdlib.h>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;

void showMenu();
unsigned long facInput;                                                 //Prototyping functions
int getInput();
float additionGetInput();
float subtractionGetInput();
float multiplicationGetInput();
float divisionGetInput();
unsigned long long factorialInput(unsigned long long facInput);
float hypotenuseFinder();



class prompt                                                            //Class so I don't have to repeatedly type the prompt.
{
public:
    prompt()
    {
        inputPromptOne = "\nEnter float one: ";
        inputPromptTwo = "\nEnter float two: ";
    }
    string inputPromptOne;
    string inputPromptTwo;
};

string returnToMain;

prompt prompt;




int main()
{

    startOfProgram:                                                      //Label for goto function.
    system("CLS");
    showMenu();
    int userInput = getInput();

    switch(userInput)
    {
    case 1:                                                                //Addition
        system("CLS");
        cout << "You chose the Addition Calculator." << endl;
        cout << "The sum is: " << additionGetInput() << endl;
        cout << "\n\nEnter 'quit' to return to menu: ";
        cin >> returnToMain;
        transform(returnToMain.begin(), returnToMain.end(), returnToMain.begin(), ::toupper);
        if(returnToMain == "QUIT")
        {
            goto startOfProgram;
        }

        break;
    case 2:                                                                 //Subtraction
        system("CLS");
        cout << "You chose the Subtraction Calculator." << endl;
        cout << "The difference is: " << subtractionGetInput() << endl;
        cout << "\n\nEnter 'quit' to return to menu: ";
        cin >> returnToMain;
        transform(returnToMain.begin(), returnToMain.end(), returnToMain.begin(), ::toupper);
        if(returnToMain == "QUIT")
        {
            goto startOfProgram;
        }
        break;
    case 3:                                                                 //Multiplication
        system("CLS");
        cout << "You chose the Multiplication Calculator." << endl;
        cout << "The product is: " << multiplicationGetInput() << endl;
        cout << "\n\nEnter 'quit' to return to menu: ";
        cin >> returnToMain;
        transform(returnToMain.begin(), returnToMain.end(), returnToMain.begin(), ::toupper);
        if(returnToMain == "QUIT")
        {
            goto startOfProgram;
        }
        break;
    case 4:                                                                 //Division
        system("CLS");
        cout << "You chose the Division Calculator." << endl;
        cout << "The quotient is: " << divisionGetInput() << endl;
        cout << "\n\nEnter 'quit' to return to menu: ";
        cin >> returnToMain;
        transform(returnToMain.begin(), returnToMain.end(), returnToMain.begin(), ::toupper);
        if(returnToMain == "QUIT")
        {
            goto startOfProgram;
        }
        break;
    case 5:
        system("CLS");                                                      //Factorial Finder
        cout << "You chose the Factorial Calculator." << endl;
        cout << "Enter a number (MAX 65): ";
        cin >> facInput;
        cout << "The factorial is: " << factorialInput(facInput) << endl;
        cout << "\n\nEnter 'quit' to return to menu: ";
        cin >> returnToMain;
        transform(returnToMain.begin(), returnToMain.end(), returnToMain.begin(), ::toupper);
        if(returnToMain == "QUIT")
        {
            goto startOfProgram;
        }
        break;
    case 6:                                                                 //Pythagorean Theorem
        system("CLS");
        cout << "You chose the Pythagorean Theorem Calculator." << endl;
        cout << "Length of side c (hypotenuse): " << hypotenuseFinder() << endl;
        cout << "\n\nEnter 'quit' to return to menu: ";
        cin >> returnToMain;
        transform(returnToMain.begin(), returnToMain.end(), returnToMain.begin(), ::toupper);
        if(returnToMain == "QUIT")
        {
            goto startOfProgram;
        }
        break;
    default:
        cout << "That is an invalid function. Try again." << endl;
        goto startOfProgram;


    }

}
void showMenu()
{
    cout << "1-Addition" << endl;
    cout << "2-Subtraction" << endl;
    cout << "3-Multiplication" << endl;
    cout << "4-Division" << endl;
    cout << "5-Factorial" << endl;
    cout << "6-Pythagorean Theorem" << endl;
    cout << "---------------------" << endl;
}
int getInput()
{
    int userInput;

    cout << "Choose a function: ";
    cin >> userInput;
    return userInput;
}
float additionGetInput()
{
    float addInput1;
    float addInput2;

    cout << prompt.inputPromptOne;
    cin >> addInput1;
    cout << prompt.inputPromptTwo;
    cin >> addInput2;
    system("CLS");

    float sum = addInput1 + addInput2;
    return sum;
}
float subtractionGetInput()
{
    float subInput1;
    float subInput2;

    cout << prompt.inputPromptOne;
    cin >> subInput1;
    cout << prompt.inputPromptTwo;
    cin >> subInput2;
    system("CLS");

    float difference = subInput1 - subInput2;
    return difference;
}
float multiplicationGetInput()
{
    float mulInput1;
    float mulInput2;

    cout << prompt.inputPromptOne;
    cin >> mulInput1;
    cout << prompt.inputPromptTwo;
    cin >> mulInput2;
    system("CLS");

    float product = mulInput1 * mulInput2;
    return product;
}
float divisionGetInput()
{
    float divInput1;
    float divInput2;

    cout << prompt.inputPromptOne;
    cin >> divInput1;
    cout << prompt.inputPromptTwo;
    cin >> divInput2;
    system("CLS");

    float quotient = divInput1 / divInput2;
    return quotient;
}
unsigned long long factorialInput(unsigned long long facInput)
{
    if(facInput==1)
    {cout << "---------------------" << endl;
        return 1;
    }
    else
    {
        return facInput*factorialInput(facInput-1);
    }
}
float hypotenuseFinder()
{
    float a;
    float b;

    cout << "Enter length of side a: ";
    cin >> a;
    cout << "\nEnter length of side b: ";
    cin >> b;

    float hypotenuse = sqrt(pow(a, 2) + pow(b, 2));
    return hypotenuse;
}

1 个答案:

答案 0 :(得分:0)

beginend方法来自C ++ STL迭代器API。

您复制的示例可以更清晰(并且不太理想)用lambdas表示(running example on ideone):

std::string input = "Yo Dogg";
std::string output(input);
std::transform(input.begin(), input.end(),
               output.begin(),
            [](auto character) {
    return ::toupper(character);
});
/// 'output' will contain "YO DOGG"

... <algorithm>库中的大多数函数都使用C ++迭代器。