C++基本语法

简介: C++基本语法

一、输出语句


#include <iostream>
#include <cstdio> //C++风格
int main(void)
{
    //输出语句
    std::cout << "hello world!" << std::endl;
    printf("hello world!\n");
    return 0;
}


二、命名空间


#include <iostream>
namespace ns1{
    void func(void){
        std::cout << "ns1的func" << std::endl;
    }
}
namespace ns2{
    void func(void){
        std::cout << "ns2的func" << std::endl;
    }
}
int main(void)
{
    //func();//名字空间成员不能直接访问
    //通过"空间::"访问
    ns1::func();
    ns2::func();
    return 0;
}


#include <iostream>
//标准名字空间指令,以后省略"std::"
using namespace std;
namespace ns1{
    void func(void){
        cout << "ns1的func" << endl;
    }
}
namespace ns2{
    void func(void){
        cout << "ns2的func" << endl;
    }
}
int main(void)
{
    using namespace ns1;//名字空间指令
    func();//ns1的func
    using namespace ns2;//名字空间指令
    //func();//歧义错误
    ns2::func();
    return 0;
}


#include <iostream>
//标准名字空间指令,以后省略"std::"
using namespace std;
namespace ns1{
    void func(void){
        cout << "ns1的func" << endl;
    }
}
namespace ns2{
    void func(void){
        cout << "ns2的func" << endl;
    }
}
int main(void)
{
    using ns1::func;//名字空间声明
    func();
    //using ns2::func;
    //func();//歧义错误
    using namespace ns2;//名字空间指令
    func();//ns1的func,局部优先
    ns2::func();//ns2的func
    return 0;
}


#include <iostream>
using namespace std;
namespace ns1{
    int num = 100;
}
namespace ns2{
    int num = 200;
}
//int num = 300;//全局作用域
namespace { //无名名字空间
    int num = 300;
}
int main(void)
{
    cout << num << endl;//300
    using ns1::num;//名字空间声明
    cout << num << endl;//100
    cout << ns2::num << endl;//200
    cout << ::num << endl;//300
    return 0;
}


三、结构体


#include <iostream>
using namespace std;
struct Teacher{
    //成员变量
    char name[20];
    int age;
    double salary;
    //成员函数
    void who(void){
        cout << "我叫" << name << ",今年" <<
            age << "岁,工资是" << salary <<
            endl;
    }
};
int main(void)
{
    /*struct*/
    Teacher wangjl = {"王建立",48,800.5};
    wangjl.who();
    return 0;
}


四、联合体


#include <iostream>
#include <cstdio>
using namespace std;
int main(void)
{
    union{//匿名联合
        unsigned int ip1;
        unsigned char ip2[4];
    };
    ip1 = 0x12345678;
    for(int i=0;i<4;i++){
        printf("%#x ",ip2[i]);    
    }
    printf("\n");
    return 0;
}


五、枚举


#include <iostream>
using namespace std;
int main(void)
{
    enum Color{RED,YELLOW,BLUE};
    cout << RED << ',' << YELLOW << ','
        << BLUE << endl;//0 1 2
    /*enum*/ Color c;
    //c = 2;//C:ok  C++:error
    c = BLUE;//C:ok  C++:ok
    cout << c << endl;//2
    return 0;
}


六、字符串


#include <iostream>
#include <cstring>
using namespace std;
int main(void)
{
    //定义
    string s = "hello";
    cout << "s=" << s << endl;
    //拷贝
    string s2;
    s2 = s;
    cout << "s2=" << s2 << endl;
    //连接
    string s3 = " world";
    s3 = s + s3;
    cout << "s3=" << s3 << endl;
    s += " world";//s = s + " world"
    cout << "s=" << s << endl;
    //比较
    if(s == s3){
        cout << "比较成立" << endl;
    }
    else{
        cout << "比较不成立" << endl;
    }
    //获取字符串中某个字符
    s[0] = 'H';
    s[6] = 'W';
    cout << s << endl;//Hello World
    //获取字符串长度
    cout << s.size() << endl;//11
    cout << s.length() << endl;//11
    //转换为C风格的字符串
    cout << strlen(s.c_str()) << endl;//11
    return 0;
}


#include <iostream>
using namespace std;
int main(void)
{
    string str;
    cout << "请输入一个字符串:" << endl;
    //cin >> str;//碰到空白符结束
    getline(cin,str);//碰到"\n"结束
    int count = 0;
    for(int i=0;i<str.size();i++){
        if(str[i]=='A' || str[i]=='a'){
            ++count;
        }
    }
    cout << "包含A/a的个数是:" << count << endl;
    return 0;
}
#include <iostream>
using namespace std;
int main(void)
{
    string str;
    cin >> str;
    int len = str.size();
    //"abcdef"==>"fedcba"
    for(int i=0; i<len/2; i++){
        str[i] = str[i] ^ str[len-1-i];
        str[len-1-i] = str[i] ^ str[len-1-i];
        str[i] = str[i] ^ str[len-1-i];
    }
    cout << str << endl;
    return 0;
    //char c1 = 3;
    //char c2 = 5;
    //c1 = c1 ^ c2;
    //c2 = c1 ^ c2;
    //c1 = c1 ^ c2;
}
目录
相关文章
|
6天前
|
Java C# C++
C++ 11新特性之语法甜点1
C++ 11新特性之语法甜点1
18 4
|
6天前
|
编译器 C++ 容器
C++ 11新特性之语法甜点2
C++ 11新特性之语法甜点2
14 1
|
6天前
|
存储 算法 编译器
C++ 11新特性之语法甜点4
C++ 11新特性之语法甜点4
11 0
|
6天前
|
安全 C++ 容器
C++ 11新特性之语法甜点3
C++ 11新特性之语法甜点3
16 0
|
2月前
|
编译器 C++ 容器
C++语言的基本语法
想掌握一门编程语言,第一步就是需要熟悉基本的环境,然后就是最重要的语法知识。 C++ 程序可以定义为对象的集合,这些对象通过调用彼此的方法进行交互。现在让我们简要地看一下什么是类、对象,方法、即时变量。 对象 - 对象具有状态和行为。例如:一只狗的状态 - 颜色、名称、品种,行为 - 摇动、叫唤、吃。对象是类的实例。 类 - 类可以定义为描述对象行为/状态的模板/蓝图。 方法 - 从基本上说,一个方法表示一种行为。一个类可以包含多个方法。可以在方法中写入逻辑、操作数据以及执行所有的动作。 即时变量 - 每个对象都有其独特的即时变量。对象的状态是由这些即时变量的值创建的。 完整关键字
54 2
|
3月前
|
Java 编译器 程序员
C++中的语法知识虚继承和虚基类
**C++中的多继承可能导致命名冲突和数据冗余,尤其在菱形继承中。为解决这一问题,C++引入了虚继承(virtual inheritance),确保派生类只保留虚基类的一份实例,消除二义性。虚继承通过`virtual`关键字指定,允许明确访问特定路径上的成员,如`B::m_a`或`C::m_a`。这样,即使基类在继承链中多次出现,也只有一份成员副本,简化了内存布局并避免冲突。虚继承应在需要时提前在继承声明中指定,影响到从虚基类派生的所有后代类。**
60 7
|
3月前
|
编译器 C++ 开发者
C++一分钟之-属性(attributes)与属性语法
【7月更文挑战第3天】C++的属性(attributes)自C++11起允许附加编译器指令,如`[[nodiscard]]`和`[[maybe_unused]]`,影响优化和警告。注意属性放置、兼容性和适度使用,以确保代码清晰和可移植。示例展示了如何使用属性来提示编译器处理返回值和未使用变量,以及利用编译器扩展进行自动清理。属性是提升代码质量的工具,但应谨慎使用。
92 13
|
4月前
|
编译器 程序员 C++
C++一分钟之-属性(attributed)与属性语法
【6月更文挑战第28天】C++的属性为代码添加元数据,帮助编译器理解意图。C++11引入属性语法`[[attribute]]`,但支持取决于编译器。常见属性如`nodiscard`提示检查返回值,`maybe_unused`防止未使用警告。问题包括兼容性、过度依赖和误用。使用属性时需谨慎,确保团队共识,适时更新以适应C++新特性。通过示例展示了`nodiscard`和`likely/unlikely`的用法,强调正确使用属性能提升代码质量和性能。
64 13
|
4月前
|
编译器 C语言 C++
下一篇
无影云桌面