Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无效打印(矢量<;字符串>;)函数未打印_C++_String_Vector_Stdvector_Cout - Fatal编程技术网

C++ 无效打印(矢量<;字符串>;)函数未打印

C++ 无效打印(矢量<;字符串>;)函数未打印,c++,string,vector,stdvector,cout,C++,String,Vector,Stdvector,Cout,我制作了一个函数,用于使用Interator打印向量。该函数是程序的一部分,用于将字符串向量复制到另一个字符串向量。最初的函数是一个使用.at()成员函数的简单for循环,这很有效。所以,我不确定这个有什么问题。 守则: #include <string> #include <string> #include <fstream> #include <iostream> #include <vector> #include <alg

我制作了一个函数,用于使用Interator打印向量。该函数是程序的一部分,用于将字符串向量复制到另一个字符串向量。最初的函数是一个使用.at()成员函数的简单for循环,这很有效。所以,我不确定这个有什么问题。 守则:

#include <string>
#include <string>
#include <fstream>
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

void print(vector<string> &v);

int main() {
    ifstream inFS;
    inFS.open("sentence.txt");

    vector<string> wordList; vector<string> newVect; 
    string s;
    while (inFS >> s) {
        wordList.push_back(s);
    }

    copy(wordList.begin(), wordList.end(), newVect.begin());
    print(wordList);
    print(newVect);
}


void print(vector<string> &v) {
    for (vector<string>::iterator i = v.begin(); i != v.end(); i++) {
        cout << *i << " ";
    }
    cout << endl;
}

尝试这个简单的更改来初始化newVect。如注释所述,还可以调整()newVect的大小

#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
无效打印(矢量和v);
int main(){
IFS;
inFS.open(“句子.txt”);
向量词表;
字符串s;
而(inFS>>s){
单词列表。推回;
}
向量newVect(单词表);
打印(字表);
打印(newVect);
}
无效打印(矢量和v){
对于(向量::迭代器i=v.begin();i!=v.end();i++){

cout Before
copy
你需要确保
newVect
有足够的元素。添加
newVect.resize(wordList.size());
Before
copy
。太棒了,谢谢你,它成功了。有什么问题吗?复制不对。正确的使用是非原创的,哦,对不起。我刚刚做了。再次感谢。
C:\Users\westt\Documents\PROJECT>a.exe

C:\Users\westt\Documents\PROJECT>
#include <string>
#include <string>
#include <fstream>
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

void print(vector<string> &v);

int main() {
    ifstream inFS;
    inFS.open("sentence.txt");

    vector<string> wordList; 
    string s;
    while (inFS >> s) {
        wordList.push_back(s);
    }

    vector<string> newVect(wordList);
    print(wordList);
    print(newVect);
}


void print(vector<string> &v) {
    for (vector<string>::iterator i = v.begin(); i != v.end(); i++) {
        cout << *i << " ";
    }
    cout << endl;
}