C++ 检测输入是 Int 还是 String

标签 c++ string csv int string-parsing

我是一名来自 Java 的 C++ 新手,因此我需要一些指导来解决我在学习过程中遇到的一些非常基本的问题。

我正在从文件中读取行,每行由 6 个字符串/整数组成,它们将作为参数发送到临时变量。

示例:

Local1,Local2,ABC,200,300,asphalt

但是,变量有两种子类型。其中最后一个参数是一个字符串(如上例中的“asphalt”)。另一个有一个 int 。我有一个读取每个参数并将其发送到变量的方法,但是如何预先检测字符串的最后一位是整数还是字符串,以便我知道是否应该将其发送到 Type1 变量还是 Type2 变量?

非常感谢!

最佳答案

由于您想确定最后一列的类型,因此这应该可行:

#include <iostream>
#include <string>
#include <cstdlib>
#include <vector>
#include <sstream>
#include <cctype>
#include <algorithm>

enum Types {
    NONE,
    STRING,
    INTEGER,
    DOUBLE
};

struct Found {
    std::string string_val;
    int integer_val;
    double double_val;
    enum Types type;
};

//copied verbatim from:
//http://stackoverflow.com/a/2845275/866930
inline bool isInteger(const std::string &s) {
   if(s.empty() || ((!std::isdigit(s[0])) && (s[0] != '-') && (s[0] != '+'))) return false;

   char * p ;
   std::strtol(s.c_str(), &p, 10);

   return (*p == 0);
}

//modified slightly for decimals:
inline bool isDouble(const std::string &s) {
   if(s.empty() || ((!std::isdigit(s[0])) && (s[0] != '-') && (s[0] != '+'))) return false ;

   char * p ;
   std::strtod(s.c_str(), &p) ;

   return (*p == 0);
}

bool isNotAlpha(char c) {
    return !(std::isalpha(c));
}

//note: this searches for strings containing only characters from the alphabet
//however, you can modify that behavior yourself.
bool isString (const std::string &s) {
    std::string::const_iterator it = std::find_if(s.begin(), s.end(), isNotAlpha);
    return (it == s.end()) ? true : false;
}

void determine_last_column (const std::string& str, Found& found) {
    //reset found:
    found.integer_val = 0;
    found.double_val = 0;
    found.string_val = "";
    found.type = NONE;

    std::string temp;
    std::istringstream iss(str);

    int column = 0;
    char *p;

    while(std::getline(iss, temp, ',')) {
        if (column == 5) {
            //now check to see if the column is an integer or not:
            if (isInteger(temp)) {
                found.integer_val = static_cast<int>(std::strtol(temp.c_str(), &p, 10));
                found.type = INTEGER;
            }
            else if (isDouble(temp)) {
                found.double_val = static_cast<double>(std::strtod(temp.c_str(), &p));
                found.type = DOUBLE;
            }
            else if (isString(temp)) {
                found.string_val = temp;
                found.type = STRING;
            }
        }
        ++column;
    }

    if (found.type == INTEGER) {
        std::cout << "An integer was found: " << found.integer_val << std::endl;
    }
    else if(found.type == DOUBLE) {
        std::cout << "A double was found: " << found.double_val << std::endl;
    }
    else if(found.type == STRING) {
        std::cout << "A string was found: " << found.string_val << std::endl;
    }
    else {
        std::cout << "A valid type was not found! Something went wrong..." << std::endl;
    }
}

int main() {
    std::string line_t1 = "Local1,Local2,ABC,200,300,asphalt";
    std::string line_t2 = "Local1,Local2,ABC,200,300,-7000.3";

    Found found;

    determine_last_column(line_t1, found);
    determine_last_column(line_t2, found);

    return 0;
}

这会输出并正确分配适当的值:

A string was found: asphalt
An integer was found: -7000.3

此版本适用于int、double、string不需要提升;并且,是普通的 C++98。

引用文献:

更新:

除了字符串之外,此版本现在还支持整数或 double 的正数和负数。

关于C++ 检测输入是 Int 还是 String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19622041/

相关文章:

c++ - 三元运算符的函数指针定义

string - Scala 实习 : how does different initialisation affect comparison?

python - 将基于列的字符串替换为多个预定义值 - Python

java - 如何将字符串分数 "2/6"转换为两个整数? java

python - 在 python 中将 CSV 转换为 JSON 文件

Python DictReader - 跳过缺少列的行?

c++ - 内部链接规则能否破坏 c++11 中的有效 c++03 代码?

c++ - 需要推导参数*之前*用户参数的模板参数

c++ - 为什么越界指针算术未定义行为?

javascript - 将csv加载到nvd3中以制作离散条形图