对于学校作业,我试图使用Employee对象的唯一指针向量来访问Employee数据,但无法弄清楚语法/编译器错误.谁能告诉我我做错了什么?必须以这种方式使用智能指针的向量.
以下是适用的代码:
// Create an Employee
Employee EmpRec;
// Assign value to a uniqueptr
unique_ptr<Employee> TempEmp;
*TempEmp = EmpRec;
// Create a vector of unique_ptr<Employee>
vector<unique_ptr<Employee>> EmpVect;
// Push the TempEmp pointer onto the vector
EmpVect.push_back(TempEmp);
// Iterate through vector, calling display function
//that prints the values of various data inside the Employee object
for (size_t i = 0; i < EmpVect.size(); ++i){
(EmpVect[i])->display(cout);
}
Run Code Online (Sandbox Code Playgroud)
这就是我的Display功能的定义方式:
void display(std::ostream& cout) const{
// print data members using cout <<
}
Run Code Online (Sandbox Code Playgroud)
在尝试编译时,我收到以下错误:
d:\ …
我有一个:
map<string, map<int,int>>
Run Code Online (Sandbox Code Playgroud)
有没有办法按字母顺序打印此地图的内容,但不区分大小写?例如,按以下顺序打印:
A : 1:1, 2:2
a : 3:1
an : 2:1
And : 4:1
and : 3:1
Run Code Online (Sandbox Code Playgroud)
目前,我正在使用以下方法进行打印:
for (auto it = tokens.begin(); it != tokens.end(); ++it){
cout << it->first << " : ";
auto const &internal_map = it->second;
for (auto it2 = internal_map.begin(); it2 != internal_map.end(); ++it2){
if (it2 != internal_map.begin())
cout << " , ";
cout << it2->first << ":" << it2->second;
}
cout << endl;
}
Run Code Online (Sandbox Code Playgroud)
这将打印所有内容,但是,它首先通过所有大写字母,然后是所有小写字母。例如:
A : 1:1, 2:2
And : 4:1 …Run Code Online (Sandbox Code Playgroud) 我有一个基本上读取文本文件的程序,并计算每行上每个单词的出现次数.使用ifstream从文本文件中读取时,一切正常,但是,如果未在命令行中输入文件名,我需要从stdin读取.
我目前使用以下内容打开并读取文件:
map<string, map<int,int>,compare> tokens;
ifstream text;
string line;
int count = 1;
if (argc > 1){
try{
text.open(argv[1]);
}
catch (runtime_error& x){
cerr << x.what() << '\n';
}
// Read file one line at a time, replacing non-desired char's with spaces
while (getline(text, line)){
replace_if(line.begin(), line.end(), my_predicate, ' ');
istringstream iss(line);
// Parse line on white space, storing values into tokens map
while (iss >> line){
++tokens[line][count];
}
++count;
}
}
else{
while (cin) {
getline(cin, line);
replace_if(line.begin(), line.end(), …Run Code Online (Sandbox Code Playgroud) 如何打印嵌套地图的内容?我正在计算一个单词在文件中出现的次数,按行号和每行出现的次数进行报告。单词、行和每行的出现次数存储在以下容器中:
map<string, map<int, int>> tokens;
Run Code Online (Sandbox Code Playgroud)
但是,我不确定语法。我正在使用以下代码打印列出所有单词的外部地图,但也无法弄清楚如何打印内部值(行号和单词在每行上出现的次数)。我假设我可以将其内联包含在循环中for,但我不知道如何:
for (map <string, map<int, int>>::iterator it = tokens.begin(); it != tokens.end(); ++it){
cout << it->first << " : " << /* assume I can include another statement here to print the values? */ endl;
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试获得与此类似的输出:
(单词:行:出现次数,行:出现次数,...)
about : 16:1, 29:1, 166:1, 190:1, 191:1
above : 137:1
accompanied : 6:1
across : 26:1
admit : 20:1
advancing : 170:1
.
.
.
Run Code Online (Sandbox Code Playgroud)