1

我有以下代码行:

vector<string> c;
string a;
for(int i=0;i<4;i++){
   cin>>a;
   c.push_back(a);
}

如果我提供输入为:

120 美元,132 美元,435 美元,534 美元

如何分别提取整数值并将它们相加以获得总值?

4

4 回答 4

4

您可以使用例如std::getline使用逗号的自定义“行”分隔符,从字符串中删除最后一个字符 (the '$') 并用于std::stoi转换为整数:

std::vector<int> c;

for (int i = 0; i < 4; i++)
{
    std::string a;
    std::getline(std::cin, a, ',');

    a = a.substr(a.length() - 1);  // Remove trailing dollar sign

    c.push_back(std::stoi(a));
}

编辑:使用std::accumulate

int sum = std::accumulate(c.begin(), c.end(), 0);

编辑2:使用std::strtol而不是std::stoi

该函数std::stoi是最新的 C++ 标准 (C++11) 中的新函数,并且尚未在所有标准库中都支持。然后你可以使用旧的 C 函数strtol

c.push_back(int(std::strtol(a.c_str(), 0, 10)));
于 2012-11-08T08:51:44.797 回答
2

您可以使用正则表达式和流:

#include <regex>
#include <iostream>
#include <sstream>

const std::string Input("120$,132$,435$,534$");

int main(int argc, char **argv)
{
    const std::regex r("[0-9]+");
    int Result = 0;

    for (std::sregex_iterator N(Input.begin(), Input.end(), r); N != std::sregex_iterator(); ++N)
    {
        std::stringstream SS(*N->begin());
        int Current = 0;
        SS >> Current;
        Result += Current;
        std::cout << Current << '\n';
    }

    std::cout << "Sum = " << Result;

    return 0;
}

输出:

120
132
435
534
Sum = 1221

如果必须确保数字后跟 a'$'则将正则表达式更改为:"[0-9]+\\$"stringstream部分将忽略'$'数字转换中的尾随:

#include <regex>
#include <iostream>
#include <sstream>

const std::string Input("120$,132$,435$,534$,1,2,3");

int main(int argc, char **argv)
{
    const std::regex r("[0-9]+\\$");
    int Result = 0;

    for (std::sregex_iterator N(Input.begin(), Input.end(), r); N != std::sregex_iterator(); ++N)
    {
        std::stringstream SS(*N->begin());
        int Current = 0;
        SS >> Current;
        Result += Current;
        std::cout << Current << '\n';
    }

    std::cout << "Sum = " << Result;

    return 0;
}

输出:

120
132
435
534
Sum = 1221
于 2012-11-08T09:50:04.913 回答
1

如果输入不是太大(特别是如果它是单行),最简单的解决方案是将它全部打包成一个字符串,然后解析它,创建一个std::istringstream来转换每个数字字段(或使用boost::lexical_cast<>, if奇怪的是,它具有适当的语义——通常在将字符串转换为内置数字类型时会这样做)。但是,对于这么简单的事情,可以直接从流中读取:

std::istream&
ignoreDollar( std::istream& stream )
{
    if ( stream.peek() == '$' ) {
        stream.get();
    }
    return stream;
}

std::istream&
checkSeparator( std::istream& stream )
{
    if ( stream.peek() == ',' ) {
        stream.get();
    } else {
        stream.setstate( std::ios_base::failbit );
    }
    return stream;
}

std::vector<int> values;
int value;
while ( std::cin >> value ) {
    values.push_back( value );
    std::cin >> ignoreDollar >> checkSeparator;
}
int sum = std::accumulate( values.begin(), values.end(), 0 );

(在这种特殊情况下,只在循环中执行所有操作可能会更简单while。但是,操纵器是一种普遍有用的技术,并且可以在更广泛的环境中使用。)

于 2012-11-08T09:07:19.057 回答
1

一个简单的版本:

int getIntValue(const std::string& data)
{
  stringstream ss(data);
  int i=0;
  ss >> i;
  return i;
}

int getSum(std::vector<std::string>& c)
{
  int sum = 0;  
  for (auto m = c.begin(); m!= c.end(); ++m)
  {
    sum += getIntValue(*m);
  }
  return sum;
}

完毕

于 2012-11-08T09:22:05.387 回答