C++;如何将字符串的单行向量转换为整数数组 我仍然是C++的新手,我试图把一个由.txt文件所组成的向量转换成int型的数组。 #pragma once #include <iostream> #include <fstream> #include <string> #include <vector> #include <functional> #include <algorithm> using namespace std; class LineCounter{ public: bool iterateFile(string fileName, function<void(const string&)> callback) { // Open the File ifstream in(fileName.c_str()); string str; // Read the next line from File until it reaches the end. while (getline(in, str)) { if (str.size() > 0) // Call the given callback callback(str); } //Close The File in.close(); return true; } int vectorMake() { vector<string> vecOfStr; //Call given lambda function for each line in file bool res = iterateFile("input-a1q1.txt", [&](const string& str) { // Add to vector vecOfStr.push_back(str); }); if (res) { vecOfStr.erase(vecOfStr.begin(), vecOfStr.begin()+3); cout << vecOfStr[0] << endl; //NEED TO CONVERT TO INT } return 0; } }; #pragma一次 #包括 #包括 #包括 #包括 #包括 #包括 使用名称空间std; 类行计数器{ 公众: bool iterateFile(字符串文件名,函数回调) { //打开文件 ifstream-in(fileName.c_str()); 字符串str; //从文件中读取下一行,直到它到达末尾。 while(getline(in,str)) { 如果(str.size()>0) //调用给定的回调函数 回调(str); } //关闭文件 in.close(); 返回true; } int vectorMake() { 向量向量; //为文件中的每一行调用给定的lambda函数 bool res=iterateFile(“input-a1q1.txt”,[&](常量字符串和str){ //添加到向量 向量推回(str); }); 如果(res) { vecOfStr.erase(vecOfStr.begin(),vecOfStr.begin()+3); cout

C++;如何将字符串的单行向量转换为整数数组 我仍然是C++的新手,我试图把一个由.txt文件所组成的向量转换成int型的数组。 #pragma once #include <iostream> #include <fstream> #include <string> #include <vector> #include <functional> #include <algorithm> using namespace std; class LineCounter{ public: bool iterateFile(string fileName, function<void(const string&)> callback) { // Open the File ifstream in(fileName.c_str()); string str; // Read the next line from File until it reaches the end. while (getline(in, str)) { if (str.size() > 0) // Call the given callback callback(str); } //Close The File in.close(); return true; } int vectorMake() { vector<string> vecOfStr; //Call given lambda function for each line in file bool res = iterateFile("input-a1q1.txt", [&](const string& str) { // Add to vector vecOfStr.push_back(str); }); if (res) { vecOfStr.erase(vecOfStr.begin(), vecOfStr.begin()+3); cout << vecOfStr[0] << endl; //NEED TO CONVERT TO INT } return 0; } }; #pragma一次 #包括 #包括 #包括 #包括 #包括 #包括 使用名称空间std; 类行计数器{ 公众: bool iterateFile(字符串文件名,函数回调) { //打开文件 ifstream-in(fileName.c_str()); 字符串str; //从文件中读取下一行,直到它到达末尾。 while(getline(in,str)) { 如果(str.size()>0) //调用给定的回调函数 回调(str); } //关闭文件 in.close(); 返回true; } int vectorMake() { 向量向量; //为文件中的每一行调用给定的lambda函数 bool res=iterateFile(“input-a1q1.txt”,[&](常量字符串和str){ //添加到向量 向量推回(str); }); 如果(res) { vecOfStr.erase(vecOfStr.begin(),vecOfStr.begin()+3); cout,c++,arrays,vector,C++,Arrays,Vector,您可以使用标准库函数,将字符串转换为整数 例如: #include <charconv> #include <iostream> #include <vector> #include <string> int main() { std::vector<std::string> src = {"0","1","2","3","4"

您可以使用标准库函数,将字符串转换为整数

例如:

#include <charconv>
#include <iostream>
#include <vector>
#include <string>

int main()
{
    std::vector<std::string> src = {"0","1","2","3","4","5","6","7"};
    std::vector<int> dst;
    for(auto s: src) {
         int i;
         std::from_chars(s.data(), s.data()+s.size() , i);
         dst.emplace_back( i );
    }

    for(auto i: dst) {
        std::cout << i << ",";
    }
    return 0;
}
#包括
#包括
#包括
#包括
int main()
{
向量src={“0”、“1”、“2”、“3”、“4”、“5”、“6”、“7”};
std::向量dst;
用于(自动s:src){
int i;
std::from_chars(s.data(),s.data()+s.size(),i);
dst.安置(i);
}
用于(自动i:dst){

std::cout Use似乎无法从chars的工作中获得。我已经包含了错误命名空间STD没有成员来自于SuxStSIT需要C++ 17,如果您使用GCC或CLANE添加命令行选项到编译器。如果您在解决方案设置中使用VisualStudio使用。如果必须使用一些旧编译器,而不需要C++ 17支持,则可以使用Fas-CARS来代替。