&dictionarylist
获取您列表的地址并打印出来。
相反,做类似的事情
std::copy(dictionarylist.begin(), dictionarylist.end(),
std::ostream_iterator<std::string>(std::cout, "\n"));
或者一个循环
for (std::string const& s: dictionarylist)
std::cout << s << "\n";
PS:无需调整列表大小(假设使用了std::list
,std::deque
等 std::vector
)。标准库容器自己进行内存管理,并push_back
会根据需要增加分配的内存。
稍微清理一下:
#include <iostream>
#include <fstream>
#include <algorithm>
#include <iterator>
#include <vector>
#include <string>
struct Words {
void readdictionary();
void printdictionary();
private:
std::vector<std::string> dictionarylist;
};
void Words::readdictionary(){
using namespace std;
ifstream file("fruitdictionary.txt");
dictionarylist.insert(
dictionarylist.end(),
istream_iterator<string>(file), {}
);
}
void Words::printdictionary(){
using namespace std;
copy(begin(dictionarylist), end(dictionarylist),
ostream_iterator<string>(cout, "\n"));
}
int main()
{
Words words;
words.readdictionary();
words.printdictionary();
}