mardi 30 juillet 2019

How to copy characters into string vector

Attempting to copy from a character vector to a string vector has been unsuccessful across multiple attempts at a solution

allocating memory to the vector prior to copying allows std::copy to work properly when placed at "OutputIterator result" (based on function template). I attempted:

std::copy(char1.begin(), char1.end(), std::back_inserter(v1));

however, this was unsuccessful as well. using back_inserter returns error c2679 "binary '=': no operator found which takes a right-hand operand of type'char' (or there is no acceptable conversion).

input file is located here: https://adventofcode.com/2018/day/2

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cstring>
#include <algorithm>
#include <iterator>
#include <cstdio>



    int main() {

        std::string field1;
        std::string field2;
        char characters;

        std::vector<char>::iterator ptr;
        std::vector<char>::iterator ptr2;

        std::vector<char> char1;
        std::vector<char> char2;

        int size = 0;

        std::ifstream inFile;

        inFile.open("C:\\Users\\Administrator\\Desktop\\c++ files\\input2.txt");

        if (!inFile) {

            std::cout << "abort";

            return 1;

        }

        while (inFile >> characters) {          //read variables from input stream

            char1.push_back(characters);

        }

        std::vector<std::string> v1(6500);

        std::copy(char1.begin(), char1.end(), std::back_inserter(v1)); 

        inFile.close();

        return 0;

    }

    //26

expect vector v1 to hold values in vector char1. I am assuming the problem stems from the data type of v1 vs. char1, however, I have not been able to find a concrete solution. I do not want to read directly into a string vector; hence my current problem.

Aucun commentaire:

Enregistrer un commentaire