zoukankan      html  css  js  c++  java
  • C++ 基础语法学习(一)

    (一)using的使用:

       最常见的是:    

    #include <iostream>
    using namespace std;

      还有更普通的:

    #include <string>
    #include
    <iostream>
    using std::string;
    using std::cin; //这三个是iostream里的
    using std::endl;
    using std::cout;
    using std::boolalpha; //解释下这个:输出bool类型的字符串类型的名字true或false
    using std::getline; //getline 读取整行文本

    //关于getline :只要 getline 遇到换行符,即便它是输入的第一个字符,getline 也将停止读入并返回。
    //如果第一个字符就是换行符,则 string 参数将被置为空 string

    (二)string 对象的操作:

    s.empty()      如果 s 为空串,则返回 true,否则返回 false

    s.size()       返回 s 中字符的个数

    s[n]           返回 s 中位置为 n 的字符,位置从 0 开始计数

    s1 + s2        s1s2 连接成一个新字符串,返回新生成的字符串

    s1 = s2        s1 内容替换为 s2 的副本

    v1 == v2       比较 v1v2的内容,相等则返回 true,否则返回 false

    下面来看个使用上面功能的例子:

     

    #include <string>
    #include
    <iostream>
    using std::string;
    using std::cin;
    using std::endl;
    using std::cout;
    using std::boolalpha;
    using std::getline;
    int main()
    {
    string line;
    string strtmp;
    // read line at time until end-of-file
    cout <<"请输入line:";
    while (getline(cin, line))
    {
    cout
    << line << endl;
    cout
    << "line的长度:" <<line.size()<<endl;
    cout
    << boolalpha << line.empty();
    cout
    << "字符串里的第六个字符:" <<line[5] << endl; //输出字符串里的第六个字符(从0开始的)
    cout <<"请输入strtmp:";
    cin
    >> strtmp;
    cout
    << "strtmp + line :" << strtmp + line << endl;
    strtmp
    = line;
    cout
    << strtmp << endl;
    cout
    << "strtmp 和 line相等吗?" << boolalpha << (strtmp == line) << endl;
    cout
    <<"请输入line:";
    }
    return 0;
    }

    (三)string 对象中字符的处理:

    isalnum(c)      如果 c 是字母或数字,则为 True

    isalpha(c)      如果 c 是字母,则为 true

    iscntrl(c)      如果 c 是控制字符,则为 true

    isdigit(c)      如果 c 是数字,则为 true

    isgraph(c)      如果 c 不是空格,但可打印,则为 true

    islower(c)      如果 c 是小写字母,则为 true

    isprint(c)      如果 c 是可打印的字符,则为 true

    ispunct(c)      如果 c 是标点符号,则 true

    isspace(c)      如果 c 是空白字符,则为 true

    isupper(c)      如果 c 是大写字母,则 true

    isxdigit(c)     如果是 c 十六进制数,则为 true

    tolower(c)      如果 c 大写字母,返回其小写字母形式,否则直接返回 c

    toupper(c)      如果 c 是小写字母,则返回其大写字母形式,否则直接返回 c

    同样的,咱们来看个使用上面功能的例子:

     

    #include <string>
    #include
    <iostream>
    using std::string;
    using std::cin;
    using std::endl;
    using std::cout;
    using std::boolalpha;
    int main()
    {
    string line;
    cout
    <<"请输入line:";
    while (cin >> line)
    {
    bool tmp;
    tmp
    = isalnum(line[0]); //功能都对应着上面的
    cout
    << boolalpha << tmp <<endl;
    tmp
    = isalpha(line[1]);
    cout
    << boolalpha << tmp <<endl;
    tmp
    = iscntrl(line[2]);
    cout
    << boolalpha << tmp <<endl;
    tmp
    = isdigit(line[3]);
    cout
    << boolalpha << tmp <<endl;
    tmp
    = islower(line[4]);
    cout
    << boolalpha << tmp <<endl;
    tmp
    = isprint(line[5]);
    cout
    << boolalpha << tmp <<endl;
    tmp
    = ispunct(line[6]);
    cout
    << boolalpha << tmp <<endl;
    tmp
    = isspace(line[8]);
    cout
    << boolalpha << tmp <<endl;
    string strtmp;
    for (int i = 0; i < 10; i++)
    {
    strtmp[i]
    = toupper(line[i]);
    cout
    << strtmp[i];
    }
    cout
    <<endl;
    cout
    <<"请输入line:";
    }
    return 0;
    }

    未完待续...................

    作者:涵曦www.hanxi.cc
    出处:hanxi.cnblogs.com
    GitHub:github.com/hanxi
    Email:im.hanxi@gmail.com
    文章版权归本人所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

    《 Skynet 游戏服务器开发实战》

  • 相关阅读:
    代码:城市名称的联想下拉框。可按拼音搜索、按汉字搜索,是一种很简单的实现方式
    代码:拖拽
    插件:zTree
    代码:遍历
    学习笔记:Stage.js(又叫Cut.js)——2D canvas 开发库,游戏方面的
    前端模块化、构建工具
    二级联动下拉菜单
    thinkphp的目录结构设计经验总结
    tp 路径表示
    liunx 根目录介绍
  • 原文地址:https://www.cnblogs.com/hanxi/p/Cplusprimer.html
Copyright © 2011-2022 走看看