补充编程// map::find #include <iostream> #include <map> #include <string> #include <vector> using namespace std; void count(string str,int a[]){ } int main () { string str; cin>>str; int a[26]={0}; count(str,a); for(int i=0;i<26;i++) if(a[i]>0){ char ch='a'+i; cout<<ch<<" "<<a[i]<<endl; } return 0; }来完成任务:输入一串小写字母组成的字符串,按字母表顺序输出各个字母出现的次数。
时间: 2023-11-27 22:55:01 浏览: 28
你可以使用 `map` 来记录每个字母出现的次数,然后按照字母表顺序输出结果。下面是代码示例:
```c++
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
void count(string str, map<char, int>& m){
for(int i=0; i<str.length(); i++){
m[str[i]]++;
}
}
int main () {
string str;
cin>>str;
map<char, int> m;
count(str, m);
for(char ch='a'; ch<='z'; ch++){
if(m.find(ch) != m.end()){
cout<<ch<<" "<<m[ch]<<endl;
}
}
return 0;
}
```
在 `count` 函数中,我们遍历字符串 `str` 的每个字符,然后将其出现次数加一,保存在 map 中。在主函数中,我们遍历每个字母,如果它在 map 中出现过,就输出它的出现次数。注意,我们使用 `m.find(ch) != m.end()` 来判断某个字母是否出现过,因为如果它没有出现过,`m[ch]` 的值将是默认值0,这样就会误判。
相关问题
请补充这个编程使它能完成题目“输入一串小写字母组成的字符串,按字母表顺序输出各个字母出现的次数。”// map::find #include <iostream> #include <map> #include <string> #include <vector> using namespace std; void count(string str,int a[]){ } int main () { string str; cin>>str; int a[26]={0}; count(str,a); for(int i=0;i<26;i++) if(a[i]>0){ char ch='a'+i; cout<<ch<<" "<<a[i]<<endl; } return 0; }
可以使用STL中的map来实现,具体代码如下:
```c++
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
void count(string str, map<char, int>& m){
for(int i=0;i<str.length();i++){
if(m.find(str[i]) == m.end()){
m[str[i]] = 1;
}else{
m[str[i]]++;
}
}
}
int main () {
string str;
cin>>str;
map<char, int> m;
count(str, m);
for(auto p : m){
cout<<p.first<<" "<<p.second<<endl;
}
return 0;
}
```
在count函数中,使用map来记录每个字母出现的次数。在遍历字符串时,如果当前字母在map中不存在,则将其加入map,并将出现次数初始化为1;否则,将其出现次数加1。最后,在主函数中遍历map输出结果。
模拟毕业生选择指导老师提供的毕业课题老师可增加,删除,修改课题,学生可选择课题,老师和学生都可以查看当前已经选择的结果 以#include<iostream> #include<string>为头文件
好的,这是一个比较复杂的问题,需要设计一些类与函数来实现。以下是一个简单的实现示例:
```c++
#include <iostream>
#include <string>
#include <vector>
#include <map>
using namespace std;
class Teacher {
private:
string name;
vector<string> topics; // 老师开设的课题列表
public:
Teacher(string name) {
this->name = name;
}
void addTopic(string topic) {
topics.push_back(topic);
}
void removeTopic(string topic) {
for (auto it = topics.begin(); it != topics.end(); ++it) {
if (*it == topic) {
topics.erase(it);
break;
}
}
}
void modifyTopic(string oldTopic, string newTopic) {
for (auto it = topics.begin(); it != topics.end(); ++it) {
if (*it == oldTopic) {
*it = newTopic;
break;
}
}
}
string getName() {
return name;
}
vector<string> getTopics() {
return topics;
}
};
class Student {
private:
string name;
string selectedTopic;
public:
Student(string name) {
this->name = name;
}
void selectTopic(string topic) {
selectedTopic = topic;
}
string getSelectedTopic() {
return selectedTopic;
}
};
class GraduationProject {
private:
vector<Teacher*> teachers;
map<string, Student*> students;
public:
void addTeacher(Teacher* teacher) {
teachers.push_back(teacher);
}
void removeTeacher(Teacher* teacher) {
for (auto it = teachers.begin(); it != teachers.end(); ++it) {
if (*it == teacher) {
teachers.erase(it);
break;
}
}
}
void modifyTopic(Teacher* teacher, string oldTopic, string newTopic) {
teacher->modifyTopic(oldTopic, newTopic);
}
void displayAllTopics() {
for (auto teacher : teachers) {
cout << "Teacher " << teacher->getName() << " topics:" << endl;
for (auto topic : teacher->getTopics()) {
cout << " " << topic << endl;
}
}
}
void selectTopic(Student* student, string topic) {
if (students.find(student->getName()) == students.end()) {
students[student->getName()] = student;
}
students[student->getName()]->selectTopic(topic);
}
void displayAllSelections() {
cout << "Student selections:" << endl;
for (auto it = students.begin(); it != students.end(); ++it) {
cout << " " << it->first << ": " << it->second->getSelectedTopic() << endl;
}
}
};
int main() {
GraduationProject project;
Teacher *t1 = new Teacher("Tom");
t1->addTopic("Topic 1");
t1->addTopic("Topic 2");
Teacher *t2 = new Teacher("Jerry");
t2->addTopic("Topic 3");
t2->addTopic("Topic 4");
project.addTeacher(t1);
project.addTeacher(t2);
Student *s1 = new Student("Alice");
project.selectTopic(s1, "Topic 2");
Student *s2 = new Student("Bob");
project.selectTopic(s2, "Topic 3");
project.displayAllTopics();
project.displayAllSelections();
return 0;
}
```
这个程序实现了老师添加、删除、修改课题,学生选择课题,以及展示所有老师的课题和所有学生的选择。你可以根据需要进行修改和扩展。