《C++ Primer》导学系列:第 8 章 - IO库

news2024/10/8 17:20:20

8.1 IO类

C++标准库提供了一套丰富的输入输出(IO)类,用于处理数据的输入输出操作。这些类位于<iostream>头文件中,包括处理标准输入输出的istreamostream类,处理文件输入输出的ifstreamofstream类,以及处理字符串流的istringstreamostringstream类。

8.1.1 istream和ostream

istream类和ostream类分别用于处理输入和输出操作。cinistream类的对象,用于从标准输入读取数据,coutostream类的对象,用于向标准输出写入数据。

示例代码
#include <iostream>

int main() {
    int number;
    std::cout << "Enter a number: ";
    std::cin >> number;  // 从标准输入读取数据
    std::cout << "You entered: " << number << std::endl;  // 向标准输出写入数据
    return 0;
}

在这个示例中,使用cin从标准输入读取一个整数,并使用cout将其输出到标准输出。

8.1.2 ifstream和ofstream

ifstream类和ofstream类分别用于处理文件的输入和输出操作。通过创建这些类的对象,可以打开文件进行读写操作。

示例代码
#include <iostream>
#include <fstream>
#include <string>

int main() {
    // 写入文件
    std::ofstream outFile("example.txt");
    if (outFile.is_open()) {
        outFile << "Hello, world!" << std::endl;
        outFile.close();
    } else {
        std::cerr << "Unable to open file for writing" << std::endl;
    }

    // 读取文件
    std::ifstream inFile("example.txt");
    if (inFile.is_open()) {
        std::string line;
        while (std::getline(inFile, line)) {
            std::cout << line << std::endl;
        }
        inFile.close();
    } else {
        std::cerr << "Unable to open file for reading" << std::endl;
    }

    return 0;
}

在这个示例中,使用ofstream对象将字符串写入文件,然后使用ifstream对象从文件读取字符串并输出到标准输出。

8.1.3 stringstream

istringstream类和ostringstream类分别用于处理字符串的输入和输出操作。这些类允许我们将字符串视为一个输入输出流进行处理。

示例代码
#include <iostream>
#include <sstream>
#include <string>

int main() {
    std::string data = "123 456 789";
    std::istringstream iss(data);
    int a, b, c;
    iss >> a >> b >> c;  // 从字符串流中读取数据

    std::cout << "Read from stringstream: " << a << " " << b << " " << c << std::endl;

    std::ostringstream oss;
    oss << a << " " << b << " " << c;  // 向字符串流中写入数据
    std::string result = oss.str();

    std::cout << "Written to stringstream: " << result << std::endl;

    return 0;
}

在这个示例中,使用istringstream对象从字符串中读取整数,然后使用ostringstream对象将整数写入到另一个字符串中。

8.1.4 IO操作的条件状态

在进行IO操作时,了解并处理流的状态非常重要。流的状态可以通过以下成员函数来检查(完整的函数请参见书本P279中的表8.2):

  • good(): 流未发生错误
  • eof(): 已到达文件末尾
  • fail(): 读/写操作失败
  • bad(): 流已崩溃

每个流对象都包含一个iostate类型的值来表示当前的状态。

示例代码
#include <iostream>
#include <fstream>

int main() {
    std::ifstream inFile("example.txt");
    if (!inFile) {
        std::cerr << "Unable to open file for reading" << std::endl;
        return 1;
    }

    int number;
    while (inFile >> number) {
        std::cout << "Read number: " << number << std::endl;
    }

    if (inFile.eof()) {
        std::cout << "End of file reached" << std::endl;
    } else if (inFile.fail()) {
        std::cerr << "Read failure" << std::endl;
    } else if (inFile.bad()) {
        std::cerr << "Bad file stream" << std::endl;
    }

    inFile.close();
    return 0;
}

在这个示例中,检查了inFile对象的状态,确保正确处理文件读取过程中可能发生的错误。

8.1.5 管理输出缓冲

输出缓冲是为了提高IO效率,将数据暂存于缓冲区,然后在适当时刻(如缓冲区满或遇到换行符)将数据输出到目的地。可以使用以下方法控制输出缓冲:

  • flush(): 强制刷新缓冲区,将所有缓冲区内容写入目的地。
  • ends: 插入一个空字符,然后刷新缓冲区。
  • endl: 插入一个换行符,然后刷新缓冲区。
示例代码
#include <iostream>
#include <fstream>

int main() {
    std::ofstream outFile("example.txt");
    if (!outFile) {
        std::cerr << "Unable to open file for writing" << std::endl;
        return 1;
    }

    outFile << "Hello, " << std::flush;
    outFile << "world!" << std::endl;

    outFile.close();
    return 0;
}

在这个示例中,使用flushendl控制输出缓冲,将数据及时写入文件。

重点与难点分析

重点

  1. 标准输入输出流:掌握cincout的基本使用方法,理解它们在标准输入输出中的作用。
  2. 文件输入输出流:掌握ifstreamofstream的使用方法,理解文件读写操作的基本流程。
  3. 字符串流:理解istringstreamostringstream的作用,掌握使用字符串流进行数据处理的方法。
  4. IO操作的条件状态:理解流的状态检查方法,掌握处理IO操作错误的方法。
  5. 管理输出缓冲:掌握控制输出缓冲的方法,理解flushendsendl的使用场景。

难点

  1. 文件操作的错误处理:理解文件操作中的错误处理机制,掌握如何检查文件打开是否成功以及如何处理读写错误。
  2. 字符串流的应用:掌握字符串流在复杂字符串处理中的应用,提高对字符串数据的处理能力。
  3. 流的状态管理:熟悉各种流状态的含义和使用场景,掌握处理不同状态的技巧。
  4. 输出缓冲的控制:理解输出缓冲的机制,掌握在不同场景下合理使用flushendsendl的方法。

练习题解析

  1. 练习8.1:编写一个程序,使用cin从标准输入读取一个整数,并使用cout将其输出到标准输出。
    • 示例代码
#include <iostream>

int main() {
    int number;
    std::cout << "Enter a number: ";
    std::cin >> number;
    std::cout << "You entered: " << number << std::endl;
    return 0;
}
  1. 练习8.2:编写一个程序,使用ofstream将一段文本写入到文件中,然后使用ifstream从该文件读取文本并输出到标准输出。
    • 示例代码
#include <iostream>
#include <fstream>
#include <string>

int main() {
    std::ofstream outFile("example.txt");
    if (outFile.is_open()) {
        outFile << "This is a test." << std::endl;
        outFile.close();
    } else {
        std::cerr << "Unable to open file for writing" << std::endl;
    }

    std::ifstream inFile("example.txt");
    if (inFile.is_open()) {
        std::string line;
        while (std::getline(inFile, line)) {
            std::cout << line << std::endl;
        }
        inFile.close();
    } else {
        std::cerr << "Unable to open file for reading" << std::endl;
    }

    return 0;
}
  1. 练习8.3:编写一个程序,使用istringstream从字符串中提取整数,并使用ostringstream将这些整数重新组合成一个字符串。
    • 示例代码
#include <iostream>
#include <sstream>
#include <string>

int main() {
    std::string data = "10 20 30";
    std::istringstream iss(data);
    int a, b, c;
    iss >> a >> b >> c;

    std::ostringstream oss;
    oss << "The numbers are: " << a << ", " << b << ", " << c;
    std::string result = oss.str();

    std::cout << result << std::endl;

    return 0;
}
  1. 练习8.4:编写一个程序,使用ifstream读取文件中的整数,并检查读取操作的条件状态。
    • 示例代码
#include <iostream>
#include <fstream>

int main() {
    std::ifstream inFile("numbers.txt");
    if (!inFile) {
        std::cerr << "Unable to open file for reading" << std::endl;
        return 1;
    }

    int number;
    while (inFile >> number) {
        std::cout << "Read number: " << number << std::endl;
    }

    if (inFile.eof()) {
        std::cout << "End of file reached" << std::endl;
    } else if (inFile.fail()) {
        std::cerr << "Read failure" << std::endl;
    } else if (inFile.bad()) {
        std::cerr << "Bad file stream" << std::endl;
    }

    inFile.close();
    return 0;
}
  1. 练习8.5:编写一个程序,使用ofstream将数据写入文件,并使用flushendl管理输出缓冲。
    • 示例代码
#include <iostream>
#include <fstream>

int main() {
    std::ofstream outFile("example.txt");
    if (!outFile) {
        std::cerr << "Unable to open file for writing" << std::endl;
        return 1;
    }

    outFile << "Hello, " << std::flush;
    outFile << "world!" << std::endl;

    outFile.close();
    return 0;
}

总结与提高

本节总结

  1. 学习了标准库中的输入输出类,理解了istreamostream的基本使用方法。
  2. 掌握了文件输入输出操作,理解了ifstreamofstream的基本使用方法。
  3. 了解了字符串流的使用,掌握了istringstreamostringstream在字符串处理中的应用。
  4. 理解了IO操作的条件状态,掌握了如何检查和处理IO操作中的错误。
  5. 学习了管理输出缓冲的方法,掌握了flushendsendl的使用。

提高建议

  1. 多练习文件操作:通过编写更多涉及文件读写操作的程序,熟悉文件操作的流程和错误处理机制。
  2. 深入理解字符串流:通过实践掌握字符串流在复杂数据处理中的应用,提高字符串数据处理能力。
  3. 扩展IO库的使用:探索更多标准库提供的IO类,如fstreamstringstream等,理解它们在不同场景中的应用。
  4. 加强流状态管理:通过实际操作和代码调试,熟悉流状态的检查和管理方法,处理IO操作中的各种异常情况。
  5. 灵活使用输出缓冲:在不同场景下合理使用flushendsendl,提高程序的输出效率和效果。

8.2 文件输入输出

在C++中,文件输入输出(File I/O)是通过标准库提供的<fstream>头文件中的类来实现的。主要的类有ifstream(输入文件流)、ofstream(输出文件流)和fstream(输入输出文件流)。这些类使得文件的读写操作变得简单和方便。

8.2.1 打开和关闭文件

使用文件流对象之前,需要先打开文件。在使用完成后,应该关闭文件以释放资源。可以通过构造函数或者open方法来打开文件,通过close方法来关闭文件。

示例代码
#include <iostream>
#include <fstream>
#include <string>

int main() {
    // 打开文件进行写操作
    std::ofstream outFile("example.txt");
    if (outFile.is_open()) {
        outFile << "Hello, world!" << std::endl;
        outFile.close();  // 关闭文件
    } else {
        std::cerr << "Unable to open file for writing" << std::endl;
    }

    // 打开文件进行读操作
    std::ifstream inFile("example.txt");
    if (inFile.is_open()) {
        std::string line;
        while (std::getline(inFile, line)) {
            std::cout << line << std::endl;
        }
        inFile.close();  // 关闭文件
    } else {
        std::cerr << "Unable to open file for reading" << std::endl;
    }

    return 0;
}

在这个示例中,使用ofstream对象打开文件进行写操作,并在写入完成后关闭文件。然后使用ifstream对象打开同一个文件进行读操作,并在读取完成后关闭文件。

8.2.2 文件模式

在打开文件时,可以指定文件模式(mode)以控制文件的打开方式。常见的文件模式包括:

  • ios::in:以读模式打开文件
  • ios::out:以写模式打开文件
  • ios::app:以追加模式打开文件
  • ios::ate:以追加模式打开文件,并将文件指针移动到文件末尾
  • ios::trunc:如果文件已存在,先清空文件内容
  • ios::binary:以二进制模式打开文件

可以通过使用位运算符|组合多个文件模式。

示例代码
#include <iostream>
#include <fstream>

int main() {
    // 以写模式打开文件,如果文件已存在,则清空文件内容
    std::ofstream outFile("example.txt", std::ios::out | std::ios::trunc);
    if (outFile.is_open()) {
        outFile << "Writing to file in write mode." << std::endl;
        outFile.close();
    } else {
        std::cerr << "Unable to open file for writing" << std::endl;
    }

    // 以追加模式打开文件,文件指针在文件末尾
    std::ofstream appendFile("example.txt", std::ios::out | std::ios::app);
    if (appendFile.is_open()) {
        appendFile << "Appending to file." << std::endl;
        appendFile.close();
    } else {
        std::cerr << "Unable to open file for appending" << std::endl;
    }

    return 0;
}

在这个示例中,首先以写模式打开文件,如果文件已存在,则清空文件内容。然后以追加模式打开文件,并在文件末尾追加内容。

8.2.3 读写二进制文件

除了文本文件,C++也支持二进制文件的读写操作。可以使用ios::binary模式打开文件,并使用writeread成员函数进行二进制数据的写入和读取。

示例代码
#include <iostream>
#include <fstream>

int main() {
    // 以二进制模式写入文件
    std::ofstream outFile("binary.dat", std::ios::out | std::ios::binary);
    if (outFile.is_open()) {
        int number = 12345;
        outFile.write(reinterpret_cast<char*>(&number), sizeof(number));
        outFile.close();
    } else {
        std::cerr << "Unable to open file for writing in binary mode" << std::endl;
    }

    // 以二进制模式读取文件
    std::ifstream inFile("binary.dat", std::ios::in | std::ios::binary);
    if (inFile.is_open()) {
        int number;
        inFile.read(reinterpret_cast<char*>(&number), sizeof(number));
        std::cout << "Read number: " << number << std::endl;
        inFile.close();
    } else {
        std::cerr << "Unable to open file for reading in binary mode" << std::endl;
    }

    return 0;
}

在这个示例中,使用ofstream对象以二进制模式打开文件,并写入一个整数。然后使用ifstream对象以二进制模式打开同一个文件,并读取该整数。

8.2.4 文件定位

在文件读写操作中,可以通过文件定位操作(seek)移动文件指针,以便在文件的不同位置进行读写。常用的文件定位操作有:

  • seekg:用于输入流,移动文件指针到指定位置
  • seekp:用于输出流,移动文件指针到指定位置
  • tellg:用于输入流,返回文件指针的当前位置
  • tellp:用于输出流,返回文件指针的当前位置
示例代码
#include <iostream>
#include <fstream>

int main() {
    // 写入文件
    std::ofstream outFile("example.txt");
    if (outFile.is_open()) {
        outFile << "0123456789";
        outFile.close();
    } else {
        std::cerr << "Unable to open file for writing" << std::endl;
    }

    // 读取文件
    std::ifstream inFile("example.txt");
    if (inFile.is_open()) {
        char ch;
        inFile.seekg(5);  // 将文件指针移动到第5个字符
        inFile.get(ch);
        std::cout << "Character at position 5: " << ch << std::endl;
        inFile.close();
    } else {
        std::cerr << "Unable to open file for reading" << std::endl;
    }

    return 0;
}

在这个示例中,首先写入一串字符到文件,然后将文件指针移动到文件的第5个字符处,并读取该字符。

重点与难点分析

重点

  1. 文件打开和关闭:掌握通过构造函数或open方法打开文件,通过close方法关闭文件。
  2. 文件模式:理解不同的文件模式及其组合使用,掌握控制文件打开方式的方法。
  3. 二进制文件读写:掌握使用ios::binary模式读写二进制文件的方法,理解writeread成员函数的用法。
  4. 文件定位:理解文件指针的移动和定位方法,掌握seekgseekptellgtellp的使用。

难点

  1. 错误处理:理解文件操作中的错误处理机制,掌握如何检查文件打开是否成功以及如何处理读写错误。
  2. 文件指针定位:熟悉文件指针的移动和定位方法,掌握在不同位置进行读写操作的技巧。

练习题解析

  1. 练习8.6:编写一个程序,使用ofstream将一段文本写入到文件中,然后使用ifstream从该文件读取文本并输出到标准输出。
    • 示例代码
#include <iostream>
#include <fstream>
#include <string>

int main() {
    std::ofstream outFile("example.txt");
    if (outFile.is_open()) {
        outFile << "This is a test." << std::endl;
        outFile.close();
    } else {
        std::cerr << "Unable to open file for writing" << std::endl;
    }

    std::ifstream inFile("example.txt");
    if (inFile.is_open()) {
        std::string line;
        while (std::getline(inFile, line)) {
            std::cout << line << std::endl;
        }
        inFile.close();
    } else {
        std::cerr << "Unable to open file for reading" << std::endl;
    }

    return 0;
}
  1. 练习8.7:编写一个程序,使用ofstream将整数写入二进制文件,然后使用ifstream从该二进制文件读取整数并输出到标准输出。
    • 示例代码
#include <iostream>
#include <fstream>

int main() {
    std::ofstream outFile("binary.dat", std::ios::out | std::ios::binary);
    if (outFile.is_open()) {
        int number = 12345;
        outFile.write(reinterpret_cast<char*>(&number), sizeof(number));
        outFile.close();
    } else {
        std::cerr << "Unable to open file for writing in binary mode" << std::endl;
    }

    std::ifstream inFile("binary.dat", std::ios::in | std::ios::binary);
    if (inFile.is_open()) {
        int number;
        inFile.read(reinterpret_cast<char*>(&number), sizeof(number));
        std::cout << "Read number: " << number << std::endl;
        inFile.close();
    } else {
        std::cerr << "Unable to open file for reading in binary mode" << std::endl;
    }

    return 0;
}
  1. 练习8.8:编写一个程序,使用ofstream将一串字符写入文件,然后使用ifstream将文件指针移动到指定位置并读取字符。
    • 示例代码
#include <iostream>
#include <fstream>

int main() {
    std::ofstream outFile("example.txt");
    if (outFile.is_open()) {
        outFile << "0123456789";
        outFile.close();
    } else {
        std::cerr << "Unable to open file for writing" << std::endl;
    }

    std::ifstream inFile("example.txt");
    if (inFile.is_open()) {
        char ch;
        inFile.seekg(5);  // 将文件指针移动到第5个字符
        inFile.get(ch);
        std::cout << "Character at position 5: " << ch << std::endl;
        inFile.close();
    } else {
        std::cerr << "Unable to open file for reading" << std::endl;
    }

    return 0;
}
  1. 练习8.9:编写一个程序,使用ofstream将数据写入文件,使用flushendl管理输出缓冲,然后使用ifstream读取文件内容并输出到标准输出。
    • 示例代码
#include <iostream>
#include <fstream>

int main() {
    std::ofstream outFile("example.txt");
    if (!outFile) {
        std::cerr << "Unable to open file for writing" << std::endl;
        return 1;
    }

    outFile << "Hello, " << std::flush;
    outFile << "world!" << std::endl;

    outFile.close();

    std::ifstream inFile("example.txt");
    if (inFile.is_open()) {
        std::string line;
        while (std::getline(inFile, line)) {
            std::cout << line << std::endl;
        }
        inFile.close();
    } else {
        std::cerr << "Unable to open file for reading" << std::endl;
    }

    return 0;
}

总结与提高

本节总结

  1. 学习了文件输入输出操作,掌握了ifstreamofstream的基本使用方法。
  2. 理解了不同的文件模式及其组合使用,掌握了控制文件打开方式的方法。
  3. 掌握了使用ios::binary模式读写二进制文件的方法,理解了writeread成员函数的用法。
  4. 理解了文件指针的移动和定位方法,掌握了seekgseekptellgtellp的使用。

提高建议

  1. 多练习文件操作:通过编写更多涉及文件读写操作的程序,熟悉文件操作的流程和错误处理机制。
  2. 深入理解文件模式:通过实践掌握不同文件模式的应用场景,提高文件读写操作的灵活性。
  3. 加强二进制文件读写能力:在实际项目中应用二进制文件读写,提高对复杂数据的处理能力。
  4. 灵活使用文件定位:通过实际操作和代码调试,掌握文件指针的移动和定位方法,在不同位置进行高效的读写操作。

8.3 string流

在C++中,string流(string stream)提供了一种在内存中操作字符串的机制。它允许我们将字符串看作一个输入输出流,使用类似文件或标准输入输出流的操作来处理字符串。标准库中的istringstreamostringstream类分别用于处理字符串的输入和输出操作,而stringstream类则兼具输入和输出功能。

8.3.1 istringstream

istringstream类用于从字符串中读取数据。它的用法类似于ifstream,但数据来源是字符串而不是文件。

示例代码
#include <iostream>
#include <sstream>
#include <string>

int main() {
    std::string data = "123 456 789";
    std::istringstream iss(data);
    int a, b, c;
    iss >> a >> b >> c;

    std::cout << "Read from stringstream: " << a << " " << b << " " << c << std::endl;
    return 0;
}

在这个示例中,istringstream对象iss从字符串data中读取三个整数,并将其输出到标准输出。

8.3.2 ostringstream

ostringstream类用于向字符串中写入数据。它的用法类似于ofstream,但数据目的地是字符串而不是文件。

示例代码
#include <iostream>
#include <sstream>

int main() {
    int a = 123, b = 456, c = 789;
    std::ostringstream oss;
    oss << a << " " << b << " " << c;

    std::string result = oss.str();
    std::cout << "Written to stringstream: " << result << std::endl;
    return 0;
}

在这个示例中,ostringstream对象oss将三个整数写入到字符串中,并将结果字符串输出到标准输出。

8.3.3 stringstream

stringstream类兼具istringstreamostringstream的功能,可以同时进行字符串的输入和输出操作。

示例代码
#include <iostream>
#include <sstream>

int main() {
    std::stringstream ss;
    ss << "Initial string content. ";

    int num = 42;
    ss << num;

    std::string output;
    ss >> output;
    std::cout << "Output from stringstream: " << output << std::endl;

    return 0;
}

在这个示例中,stringstream对象ss首先将字符串写入流中,然后将整数num写入流中。随后,从流中读取一个单词并输出。

8.3.4 string流的应用

string流在数据转换、格式化输出和解析复杂字符串等场景中非常有用。例如,可以使用string流将各种类型的数据转换为字符串,或将字符串解析为各种类型的数据。

示例代码
#include <iostream>
#include <sstream>
#include <string>

int main() {
    // 将各种数据类型转换为字符串
    int num = 123;
    double pi = 3.14;
    std::string text = "Hello";

    std::ostringstream oss;
    oss << "Number: " << num << ", Pi: " << pi << ", Text: " << text;

    std::string result = oss.str();
    std::cout << "Formatted string: " << result << std::endl;

    // 将字符串解析为各种数据类型
    std::string data = "42 3.14159 hello";
    std::istringstream iss(data);
    int i;
    double d;
    std::string s;
    iss >> i >> d >> s;

    std::cout << "Parsed values - Integer: " << i << ", Double: " << d << ", String: " << s << std::endl;

    return 0;
}

在这个示例中,使用string流将整数、浮点数和字符串转换为格式化字符串,然后将字符串解析为整数、浮点数和字符串。

重点与难点分析

重点

  1. istringstream:掌握istringstream的基本使用方法,理解其在从字符串中读取数据时的作用。
  2. ostringstream:掌握ostringstream的基本使用方法,理解其在向字符串中写入数据时的作用。
  3. stringstream:掌握stringstream的基本使用方法,理解其在同时进行字符串输入和输出时的作用。

难点

  1. 字符串流的使用场景:理解在何种场景下使用字符串流能够提高程序的灵活性和可维护性。
  2. 数据类型转换和解析:掌握使用字符串流进行各种数据类型的转换和解析,提高字符串数据处理能力。

练习题解析

  1. 练习8.10:编写一个程序,使用istringstream从字符串中读取多个整数,并将其输出到标准输出。
    • 示例代码
#include <iostream>
#include <sstream>
#include <string>

int main() {
    std::string data = "10 20 30 40 50";
    std::istringstream iss(data);
    int number;

    while (iss >> number) {
        std::cout << "Read number: " << number << std::endl;
    }

    return 0;
}
  1. 练习8.11:编写一个程序,使用ostringstream将多个数据写入到字符串中,并将结果字符串输出到标准输出。
    • 示例代码
#include <iostream>
#include <sstream>

int main() {
    std::ostringstream oss;
    int a = 1, b = 2, c = 3;
    oss << "Numbers: " << a << ", " << b << ", " << c;

    std::string result = oss.str();
    std::cout << "Resulting string: " << result << std::endl;

    return 0;
}
  1. 练习8.12:编写一个程序,使用stringstream同时进行字符串的输入和输出操作。
    • 示例代码
#include <iostream>
#include <sstream>
#include <string>

int main() {
    std::stringstream ss;
    ss << "Example string with number: " << 100;

    std::string word;
    int number;

    ss >> word >> word >> word >> word >> number;
    std::cout << "Extracted word: " << word << std::endl;
    std::cout << "Extracted number: " << number << std::endl;

    return 0;
}
  1. 练习8.13:编写一个程序,使用字符串流将不同类型的数据转换为字符串,并将字符串解析为不同类型的数据。
    • 示例代码
#include <iostream>
#include <sstream>
#include <string>

int main() {
    // 将数据转换为字符串
    int age = 25;
    double height = 5.9;
    std::string name = "John";

    std::ostringstream oss;
    oss << "Name: " << name << ", Age: " << age << ", Height: " << height;

    std::string result = oss.str();
    std::cout << "Formatted string: " << result << std::endl;

    // 将字符串解析为数据
    std::istringstream iss(result);
    std::string label;
    std::string parsedName;
    int parsedAge;
    double parsedHeight;

    iss >> label >> parsedName >> label >> parsedAge >> label >> parsedHeight;

    std::cout << "Parsed name: " << parsedName << std::endl;
    std::cout << "Parsed age: " << parsedAge << std::endl;
    std::cout << "Parsed height: " << parsedHeight << std::endl;

    return 0;
}

总结与提高

本节总结

  1. 学习了istringstream的使用,掌握了从字符串中读取数据的方法。
  2. 学习了ostringstream的使用,掌握了向字符串中写入数据的方法。
  3. 掌握了stringstream的使用,理解了其在同时进行字符串输入和输出操作中的作用。
  4. 理解了string流在数据转换和字符串解析中的应用,提高了处理复杂字符串数据的能力。

提高建议

  1. 多练习字符串流操作:通过编写涉及字符串流操作的程序,熟悉istringstreamostringstreamstringstream的使用方法。
  2. 掌握数据转换和解析技巧:通过实践掌握使用字符串流进行数据转换和解析的技巧,提高处理复杂数据的能力。
  3. 扩展字符串流的应用场景:在实际项目中应用字符串流进行数据处理,提高程序的灵活性和可维护性。

本主页会定期更新,为了能够及时获得更新,敬请关注我:点击左下角的关注。也可以关注公众号:请在微信上搜索公众号“iShare爱分享”并关注,或者扫描以下公众号二维码关注,以便在内容更新时直接向您推送。 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1858704.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Java 8 Date and Time API

Java 8引入了新的日期和时间API&#xff0c;位于java.time包下&#xff0c;旨在替代旧的java.util.Date和java.util.Calendar类。新API更为简洁&#xff0c;易于使用&#xff0c;并且与Joda-Time库的一些理念相吻合。以下是Java 8 Date and Time API中几个核心类的简要概述&…

构建开源多模态RAG系统

在这个新的冒险中&#xff0c;我们将深入研究使用开源大型语言多模态&#xff08;LLMM&#xff09;构建检索增强型生成&#xff08;RAG&#xff09;系统的过程。值得注意的是&#xff0c;我们的重点是在不依赖LangChain或Llama索引的情况下实现这一点&#xff1b;相反&#xff…

Trilium windows上修改笔记目录,创建多个笔记空间方法

一开始使用trilium会非常的不舒服&#xff0c;不像是obsidian可以创建多个笔记空间&#xff0c;指定多个笔记目录。这里摸索到了解决方案 修改目录的方法一 ——修改系统环境变量 打开控制面板-系统-高级系统设置 新增如上条目 修改目录的方法二——直接写bat脚本运行 新建位…

Scope XY Project的使用

1.Scope XY Project的功能介绍与使用方法 添加监控变量 绘制成一个三角形 XY进行对调操作 修改XY轴的比例修改显示输出 2.Cursor的使用方法 游标线的添加测量 3.Reporting功能的使用方法 到处对应的报表数据 添加对应的报告数据

React Suspense的原理

React Suspense组件的作用是当组件未完成加载时&#xff0c;显示 fallback 组件。那么 Suspense 是如何实现的呢&#xff1f;React 的渲染是通过 Fiber 进行的&#xff0c;Suspense 的更新机制也是要围绕 Fiber 架构进行的。Suspense 是由两部分组成&#xff0c;实际 UI 子组件…

Go 语言学习笔记之字典 Map

Go 语言中的字典 Map 大家好&#xff0c;我是码农先森。 概念 在 Go 语言中&#xff0c;字典被称为 map&#xff0c;它是一种无序的集合&#xff0c;用于存储键值对。每个键在 map 中必须是唯一的&#xff0c;并且对应一个值。map 是一种非常常用的数据结构&#xff0c;用于…

iptables(6)扩展匹配条件--tcp-flags、icmp

简介 前面我们已经介绍了不少的扩展模块,例如multiport、iprange、string、time、connlimit模块,但是在tcp扩展模块中只介绍了tcp扩展模块中的”--sport”与--dport”选项,并没有介绍”--tcp-flags”选项,那么这篇文章,我们就来认识一下tcp扩展模块中的”--tcp-flags”和i…

毫米波移动通信系统中的波束赋形— 基于码本的波束训练

基于码本的波束训练算法该方法在收发端都配置波束矢量的码本&#xff0c;通过波束搜索的方式发现最优的波束方向为了加快波束搜索的速度&#xff0c;往往采用逐步缩小搜索范围的方式加快搜索&#xff0c;可以将搜索算法的时间复杂度从O(N)降低到O(logN)&#xff0c;其中N表示码…

spring-gateway include-expression 配置说明

在开发过程中遇到的一些配置问题&#xff0c;记录下来以供参考 spring-gateway版本是2.2.9-release,使用的spring cloud dependence 是 Hoxton.SR12 在依赖eureka 服务发现并自动将发现服务器加入到router中的时候&#xff0c;需要指定对应的服务进行添加&#xff0c;根据文档…

NtripShare2024年第二季度主要技术进展

NtripShare Cloud GNSS解算云平台方面 1、解算引擎增加根据卫星多路径效应自动剔除卫星的算法。 2、解算引擎增加解算时间段限制&#xff08;发现贵州某地在晚12点周期性效果变差&#xff09;。 3、增加2000坐标至地方坐标系转换的支持(七参数、四参数、TGO高程拟合&#x…

GitHub星标破千!斯坦福大学的284个机器学习小抄(漫画中文版)

说到人工智能必然要了解机器学习&#xff0c;从信息化软件&#xff0c;到电子商务&#xff0c;然后到高速发展互联网时代&#xff0c;到至今的云计算、大数据等&#xff0c;渗透到我们的生活、工作之中&#xff0c;在互联网的驱动下&#xff0c;人们更清晰的认识和使用数据&…

第一百二十三节 Java面向对象的设计 - Java接口继承

Java面向对象的设计 - Java接口继承 接口可以从另一个接口继承。与类不同&#xff0c;接口可以从多个接口继承。 interface Singer {void sing();void setRate(double rate);double getRate(); } interface Writer {void write();void setRate(double rate);double getRate();…

【代码随想录刷题】day02——977.有序数组的平方 ,209.长度最小的子数组 ,59.螺旋矩阵II

977.有序数组的平方 方法一&#xff1a;暴力法 class Solution { public:vector<int> sortedSquares(vector<int>& nums) {// 暴力法for(int i 0; i < nums.size(); i){nums[i] * nums[i];}sort(nums.begin(), nums.end());return nums;} };方法二&#…

思考题:相交的几何图形

给定不超过 26 个几何图形&#xff0c;每个图形都有一个唯一大写字母作为其编号。 每个图形在平面中的具体位置已知&#xff0c;请你判断&#xff0c;对于每个图形&#xff0c;有多少个其他图形与其存在交点。 在判断交点时&#xff0c;只考虑边与边相交的情况&#xff0c;如…

探秘神经网络激活函数:Sigmoid、Tanh和ReLU,解析非线性激活函数的神奇之处

引言 在神经网络中&#xff0c;激活函数扮演着至关重要的角色。它们赋予神经网络非线性的能力&#xff0c;使得网络具备学习和表示复杂函数关系的能力。本文将详细解析三种常见的激活函数&#xff1a;Sigmoid、Tanh和ReLU&#xff0c;揭开它们在神经网络中的奥秘。无论你是初学…

【IDEA】扩大虚拟机栈空间操作

输入命令参数-Xss 要更改的空间大小

【转载】Purple Pi OH OpenHarmony开发板使用体验+原创跨屏输入应用

本文原作者&#xff1a;westinyang&#xff0c;本号已被授权仅转载 一、开发板介绍 1.1 产品概述 Purple Pi OH智能主板&#xff0c;配备Rockchip RK3566四核Cortex-A55处理器&#xff0c;主频最高1.8GHz&#xff0c;LPDDR4/LPDDR4X 默认2GB &#xff0c;最大可以支持8GB内存…

华芯微特SWM34-使用定时器捕获快速解码EV1527编码

在无线应用领域&#xff0c;很多433Mhz和315Mhz的遥控器&#xff0c;红外探测器&#xff0c;门磁报警器&#xff0c;无线门铃等都使用EV1527编码格式来发射数据。发射和接收均有对应的RF芯片完成&#xff0c;而且成本极低&#xff08;目前市场价3毛钱不到&#xff09;。接收芯片…

centos7 xtrabackup mysql 基本测试(5)mysql 建立 测试 数据库及内容

centos7 xtrabackup mysql 基本测试&#xff08;5&#xff09;mysql 建立 测试 数据库及内容 登录 mysql -u etc -p 1234aA~1创建数据库 名字是company show databases ; create database company;在 company里面 创建表employee use company; DROP TABLE IF EXISTS employ…

【Mysql】数据库事务-手动提交

数据库事务 ** 什么是事务** 事务是一个整体,由一条或者多条SQL 语句组成,这些SQL语句要么都执行成功,要么都执行失败, 只要有一条SQL出现异常,整个操作就会回滚,整个业务执行失败。 比如: 银行的转账业务,张三给李四转账500元 , 至少要操作两次数据库, 张三 -500, 李四 50…