首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

C++:尝试在单个for循环中使用getlline()填充多个向量时,向量下标超出范围

在C++中,可以使用getline()函数从输入流中读取一行文本,并将其存储到一个字符串中。如果要在单个for循环中使用getline()函数填充多个向量,可以使用istringstream类将读取的字符串转换为其他类型的数据。

下面是一个示例代码,演示了如何在单个for循环中使用getline()函数填充多个向量:

代码语言:txt
复制
#include <iostream>
#include <vector>
#include <string>
#include <sstream>

int main() {
    std::string input;
    std::vector<int> intVector;
    std::vector<float> floatVector;
    std::vector<std::string> stringVector;

    std::getline(std::cin, input); // 从输入流中读取一行文本

    std::istringstream iss(input); // 创建一个字符串流

    std::string token;
    while (std::getline(iss, token, ' ')) { // 使用空格作为分隔符,将字符串分割为多个子字符串
        int intValue;
        float floatValue;

        // 尝试将子字符串转换为整数
        try {
            intValue = std::stoi(token);
            intVector.push_back(intValue);
        } catch (const std::exception& e) {
            // 如果转换失败,则尝试将子字符串转换为浮点数
            try {
                floatValue = std::stof(token);
                floatVector.push_back(floatValue);
            } catch (const std::exception& e) {
                // 如果转换失败,则将子字符串作为字符串存储
                stringVector.push_back(token);
            }
        }
    }

    // 输出填充后的向量内容
    std::cout << "Int Vector: ";
    for (const auto& value : intVector) {
        std::cout << value << " ";
    }
    std::cout << std::endl;

    std::cout << "Float Vector: ";
    for (const auto& value : floatVector) {
        std::cout << value << " ";
    }
    std::cout << std::endl;

    std::cout << "String Vector: ";
    for (const auto& value : stringVector) {
        std::cout << value << " ";
    }
    std::cout << std::endl;

    return 0;
}

在上述代码中,我们首先使用getline()函数从输入流中读取一行文本,并将其存储到input字符串中。然后,我们创建了一个istringstream对象iss,将input作为其构造函数的参数,从而创建了一个字符串流。接下来,我们使用getline()函数和空格作为分隔符,将字符串流中的内容分割为多个子字符串。对于每个子字符串,我们首先尝试将其转换为整数,如果转换成功,则将其存储到intVector中;如果转换失败,则尝试将其转换为浮点数,如果转换成功,则将其存储到floatVector中;如果转换失败,则将其作为字符串存储到stringVector中。最后,我们分别输出填充后的intVector、floatVector和stringVector的内容。

这种方法可以在单个for循环中使用getline()函数填充多个向量,但需要注意的是,如果输入的字符串中包含无法转换为整数或浮点数的子字符串,会抛出异常。因此,在实际应用中,需要根据具体情况进行错误处理。

腾讯云相关产品和产品介绍链接地址:

  • 腾讯云C++ SDK:https://cloud.tencent.com/document/product/876/19399
  • 腾讯云云服务器(CVM):https://cloud.tencent.com/product/cvm
  • 腾讯云云数据库 MySQL 版:https://cloud.tencent.com/product/cdb_mysql
  • 腾讯云对象存储(COS):https://cloud.tencent.com/product/cos
  • 腾讯云人工智能:https://cloud.tencent.com/product/ai
  • 腾讯云物联网平台:https://cloud.tencent.com/product/iotexplorer
  • 腾讯云移动开发:https://cloud.tencent.com/product/mobdev
  • 腾讯云区块链服务:https://cloud.tencent.com/product/tbaas
  • 腾讯云元宇宙:https://cloud.tencent.com/product/tencent-metaverse
页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

  • R语言数据抓取实战——RCurl+XML组合与XPath解析

    经常有小伙伴儿跟我咨询,在使用R语言做网络数据抓取时,遇到空值和缺失值或者不存在的值,应该怎么办。 因为我们大多数场合从网络抓取的数据都是关系型的,需要字段和记录一一对应,但是html文档的结构千差万别,代码纷繁复杂,很难保证提取出来的数据开始就是严格的关系型,需要做大量的缺失值、不存在内容的判断。 如果原始数据是关系型的,但是你抓取来的是乱序的字段,记录无法一一对应,那么这些数据通常价值不大,今天我以一个小案例(跟昨天案例相同)来演示,如何在网页遍历、循环嵌套中设置逻辑判断,适时的给缺失值、不存在值填充预

    08
    领券