C++ 教程 在线

1959cpp-increment-decrement-operators

算术运算符 ++d 和 d++ 的区别:

++d先加,先对 d 的值加 1,再使用 d 的值执行该行命令。

d++后加,先使用 d 的值执行该行命令,执行完后再对 d 的值加 1。

实例:

#include <iostream>
using namespace std;
int main()
{
    int c;
    int d = 10;   //  测试自增、自减
    c = ++d;      // ++d 是先对 d 的值加 1,再使用 d 的值执行该行命令
    cout << "d 等于 " << d << endl;
    cout << "c 等于 " << c << endl ;
    int e = 10;   // 测试自增、自减  
    c = e++;      // e++ 是先使用 e 的值执行该行命令,执行完后再对 e 的值加 1
    cout << "e 等于 " << e << endl;
    cout << "c 等于 " << c << endl ;
    return 0;
}
输出结果:
d 等于 11
c 等于 11
e 等于 11
c 等于 10

自减运算符同理。

1958C++ 资源库 C++ STL 教程

C++ STL 之 vector 的 capacity 和 size 属性区别

size 是当前 vector 容器真实占用的大小,也就是容器当前拥有多少个容器。

capacity 是指在发生 realloc 前能允许的最大元素数,即预分配的内存空间。

当然,这两个属性分别对应两个方法:resize()reserve()

使用 resize() 容器内的对象内存空间是真正存在的。

使用 reserve() 仅仅只是修改了 capacity 的值,容器内的对象并没有真实的内存空间(空间是"野"的)。

此时切记使用 [] 操作符访问容器内的对象,很可能出现数组越界的问题。

下面用例子进行说明:

#include <iostream>
#include <vector>

using std::vector;
int main(void)
{
    vector<int> v;
    std::cout<<"v.size() == " << v.size() << " v.capacity() = " << v.capacity() << std::endl;
    v.reserve(10);
    std::cout<<"v.size() == " << v.size() << " v.capacity() = " << v.capacity() << std::endl;
    v.resize(10);
    v.push_back(0);
    std::cout<<"v.size() == " << v.size() << " v.capacity() = " << v.capacity() << std::endl;

    return 0;
}

运行结果为:(win 10 + VS2010)

注: 对于 reserve(10) 后接着直接使用 [] 访问越界报错(内存是野的),大家可以加一行代码试一下,我这里没有贴出来。

这里直接用[]访问,vector 退化为数组,不会进行越界的判断。此时推荐使用 at(),会先进行越界检查。

相关引申:

针对 capacity 这个属性,STL 中的其他容器,如 list map set deque,由于这些容器的内存是散列分布的,因此不会发生类似 realloc() 的调用情况,因此我们可以认为 capacity 属性针对这些容器是没有意义的,因此设计时这些容器没有该属性。

在 STL 中,拥有 capacity 属性的容器只有 vector 和 string。

1957C++ 多线程

要注意内存泄露问题。

如果设置为 PTHREAD_CREATE_JOINABLE,就继续用 pthread_join() 来等待和释放资源,否则会内存泄露。

1956C++ 多线程

c++ 11 之后有了标准的线程库:

#include <iostream>

#include <thread>

std::thread::id main_thread_id = std::this_thread::get_id();

void hello()  
{
    std::cout << "Hello Concurrent World\n";
    if (main_thread_id == std::this_thread::get_id())
        std::cout << "This is the main thread.\n";
    else
        std::cout << "This is not the main thread.\n";
}

void pause_thread(int n) {
    std::this_thread::sleep_for(std::chrono::seconds(n));
    std::cout << "pause of " << n << " seconds ended\n";
}

int main() {
    std::thread t(hello);
    std::cout << t.hardware_concurrency() << std::endl;//可以并发执行多少个(不准确)
    std::cout << "native_handle " << t.native_handle() << std::endl;//可以并发执行多少个(不准确)
    t.join();
    std::thread a(hello);
    a.detach();
    std::thread threads[5];                         // 默认构造线程

    std::cout << "Spawning 5 threads...\n";
    for (int i = 0; i < 5; ++i)
        threads[i] = std::thread(pause_thread, i + 1);   // move-assign threads
    std::cout << "Done spawning threads. Now waiting for them to join:\n";
    for (auto &thread : threads)
        thread.join();
    std::cout << "All threads joined!\n";
}

之前一些编译器使用 C++11 的编译参数是 -std=c++11

g++ -std=c++11 test.cpp -lpthread

1955C++ 信号处理

Sleep 函数

功能:执行挂起一段时间,也就是等待一段时间在继续执行

用法:Sleep(时间)

注意:

  • (1)Sleep是区分大小写的,有的编译器是大写,有的是小写。
  • (2)Sleep括号里的时间,在windows下是已毫秒为单位,而LInux是已秒为单位。
#include <iostream>
#include <windows.h>

using namespace std;

int main()
{
    int a = 1;
    while (a)
    {
        cout << "欢迎来到菜鸟教程!" << endl;
        Sleep(100);
    }
    system("pause");
    return 0;
}