std::getchar
时间: 2024-03-22 10:35:50 浏览: 18
std::getchar是C++标准库中的一个函数,用于从标准输入流(通常是键盘)读取一个字符。它的函数原型如下:
```cpp
int getchar();
```
该函数会读取一个字符,并返回其ASCII码值(整数)。如果读取失败或者到达文件末尾,它会返回特殊值EOF(End of File)。
使用std::getchar函数时,你可以按下任意一个字符,然后按下回车键来触发输入。例如,以下代码示例演示了如何使用std::getchar函数来读取用户输入的字符并打印出来:
```cpp
#include <iostream>
int main() {
std::cout << "请输入一个字符:";
int ch = std::getchar();
std::cout << "你输入的字符是:" << static_cast<char>(ch) << std::endl;
return 0;
}
```
在上述代码中,我们首先输出提示信息,然后调用std::getchar函数来读取用户输入的字符,并将其保存在变量ch中。最后,我们将该字符转换为char类型并打印出来。
相关问题
class Config { public: using Ptr_=std::shared_ptr<Config>; private: Config() = default; Config(Config &&) = delete; Config(const Config &)= delete; Config &operator=(Config &&)= delete; Config &operator=(const Config &)= delete; static Ptr_ config_info_; std::map<std::string, std::string> storage_; public: ~Config() = default; public: static Ptr_ GetInstance(); bool Open(std::string config_file); template <typename T> T Get(std::string key){ transform(key.begin(),key.end(),key.begin(),::tolower); // 转小写 if(storage_.count(key)>0){ try{ double value=stod(storage_[key]); return static_cast<T>(value); } catch (const std::exception &e){ std::cerr<<e.what()<<'\n'; } } else{ // LOG(ERROR)<<"The key of "<<key<<" does not exist"; // getchar(); return T(0x0); } } template <typename T> std::vector<T> GetArray(std::string key){ std::vector<T> data; transform(key.begin(),key.end(),key.begin(),::tolower); // 转小写 if(storage_.count(key)>0){ try{ auto text=TextSplit(storage_[key],","); for(auto index:text){ double value=stod(index); data.emplace_back(static_cast<T>(value)); } } catch(const std::exception &e){ std::cerr<<e.what()<<'\n'; } } else{ // LOG(ERROR)<<"The key of "<<key<<" does not exist"; // getchar(); } return data; } };
这段代码定义了一个名为`Config`的类。该类具有以下特点:
- `Ptr_`是一个`std::shared_ptr<Config>`类型的别名,用于方便管理`Config`类的实例。
- 构造函数`Config()`使用`default`关键字进行默认定义。
- 移动构造函数`Config(Config &&)`和拷贝构造函数`Config(const Config &)`被禁用,使用`= delete`来实现。
- 移动赋值运算符`operator=(Config &&)`和拷贝赋值运算符`operator=(const Config &)`也被禁用,使用`= delete`来实现。
- 静态成员变量`config_info_`是一个指向`Config`类实例的共享指针。
- `storage_`是一个存储键值对的映射,用于存储配置信息。
- 析构函数`~Config()`使用`default`关键字进行默认定义。
- 公有静态成员函数`GetInstance()`用于获取`Config`类的实例。
- 公有成员函数`Open(std::string config_file)`用于打开配置文件并读取配置信息。
- 成员函数模板`Get<T>(std::string key)`用于根据给定的键获取配置值,并以指定类型返回。
- 成员函数模板`GetArray<T>(std::string key)`用于根据给定的键获取配置值,并以指定类型的向量返回。
代码中还包含了一些注释和错误处理逻辑,但是这些部分被注释掉了,可能是为了调试或其他目的。
void linkcreat(childptr &L) //带头结点的链表的建立 { childnode *p,*q;L=(childptr)malloc(sizeof(childnode));L->下一个=空;p=L;cout <<“一次性输入子目录在数组中的位置(数据之间空格),输入-2结束”<< endl;/p->child=getchar();/ //输入字符会转换成ASCII值存储 std::cin>>p->child;while(p->child!=-2) { q=p; p=(childptr)malloc(sizeof(childnode)); /cout(“输入子目录位置:”);/ /p->child=getchar();/ std::cin>>p->child;p->下一个=q;} L=p;以上采用了倒序插入结点法建立链表 } 把此段代码的链表的初始化为0
这段代码本身并没有链表初始化的操作,如果要将链表初始化为0,可以在创建链表前将其初始化为NULL即可,如下所示:
```
childptr L = NULL;
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)