小编son*_*yao的帖子

C++ 嵌套作用域访问

我最近在cppreference 中看到了这段代码:

string str="global scope";

void main()
{
    string str="main scope";
    if (true){
        string str="if scope";
        cout << str << endl;
    }
    cout << str << endl;
}
Run Code Online (Sandbox Code Playgroud)

哪个输出:

if scope
main scope
Run Code Online (Sandbox Code Playgroud)

这很好,我了解整个嵌套作用域的事情,并且我知道当堆栈在语句末尾展开它时, if 作用域内的“str”将被销毁,因此此后它将不可用,因此第二个print 将主“str”作为其参数。

但是,我知道主“str”实际上在 IF 内部可用,或者至少应该是,但问题是如何从 IF 语句内部访问主“str”?

我如何从 main 和/或 if 内部访问全局“str”?

我知道使用不同的名称会更简单,但这个问题不是为了特定的实际应用,而是为了更好地理解 c++ 范围。

c++ scope nested name-hiding

4
推荐指数
1
解决办法
2249
查看次数

std :: copy_n不会更改目标向量大小

如果我为一个向量保留了一些空间,然后我用它复制了一些值std::copy_n(),我得到正确复制并可访问的值,但向量的大小仍为零.这是预期的行为吗?我应该调整矢量大小,即使它没有效率吗?

#include <algorithm>
#include <iostream>
#include <vector>

int main()
{
    std::vector<double> src, dest;

    for(double x = 0.0; x < 100.0; ++x)
        src.push_back(x);

    dest.reserve(src.size());

    std::copy_n(src.cbegin(), src.size(), dest.begin());

    std::cout << "src.size() = " << src.size() << std::endl;
    std::cout << "dest.size() = " << dest.size() << std::endl;

    for(size_t i = 0; i < src.size(); ++i)
        std::cout << dest[i] << "  ";

}
Run Code Online (Sandbox Code Playgroud)

编译器测试:clang,gcc,Visual C++

c++ copy stdvector stl-algorithm c++11

4
推荐指数
1
解决办法
573
查看次数

为什么只有在定义了类的情况下才能将数据成员指定为类类型?(摘自"C++入门"一书)

在"C++入门"一书中,有一节关于类声明和定义.我不明白这句话的一切:

只有在定义了类时,才能将数据成员指定为类类型.

我不明白这句话背后的逻辑.如何将数据成员指定为类类型,此操作的含义是什么?

c++ declaration definition incomplete-type

4
推荐指数
1
解决办法
156
查看次数

如何将任何大小的bitset传递给函数?

以下程序编译正常.

#include <iostream>
#include <bitset>

void foo(std::bitset<10> n)
{
    std::cout << n.size() << "\n";
}

int main()
{
    std::bitset<10> n;
    foo(n);
}
Run Code Online (Sandbox Code Playgroud)
$ g++ -std=c++11 -Wall -Wextra -pedantic foo.cpp
$ ./a.out 
10
Run Code Online (Sandbox Code Playgroud)

如何修改foo()函数以使其可以接受bitset任何大小?

c++ templates

4
推荐指数
1
解决办法
684
查看次数

为什么调用转发引用构造函数而不是复制构造函数?

鉴于以下代码

#include <iostream>

using namespace std;

template <typename Type>
struct Something {
    Something() {
        cout << "Something()" << endl;
    }

    template <typename SomethingType>
    Something(SomethingType&&) {
        cout << "Something(SomethingType&&)" << endl;
    }
};

int main() {
    Something<int> something_else{Something<int>{}};
    auto something = Something<int>{};
    Something<int>{something};
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我得到以下输出

Something()
Something()
Something(SomethingType&&)
Run Code Online (Sandbox Code Playgroud)

为什么复制构造函数被解析为模板化转发引用构造函数而不是移动构造函数?我猜这是因为移动构造函数是隐式定义的,而不是复制构造函数。但是在阅读了堆栈溢出中未隐式定义复制构造函数的情况后,我仍然感到困惑。

c++ constructor c++11 c++14 implicit-constructor

4
推荐指数
1
解决办法
224
查看次数

当读取错误的类型时,cin会覆盖我的初始化值?

所以这是一个非常基本的问题和超级微不足道的但我只是通过c ++中的编程原则和实践,我的程序用于读取字符串和int的行为与Bjarne Stroustrup编写的书不同,所以如果他感到惊讶犯了一个错误.无论如何这里是代码:

#include "..\std_lib_facilities.h"

int main()
{
    cout << "Please enter your first name and age\n";
    string first_name = "???"; // string variable
                               // ("???” means “don’t know the name”)
    int age = -1;              // integer variable (1 means “don’t know the age”)
    cin >> first_name >> age;  // read a string followed by an integer
    cout << "Hello, " << first_name << " (age " << age << ")\n";
}
Run Code Online (Sandbox Code Playgroud)

当我在提示符输入"22 Carlos"时它输出"Hello,22(0岁)",基本上使我的错误检查的初始化值无用.这是c ++或其他东西的新功能,这就是为什么这本书出错了?

编辑1:BTW我在Windows 7上使用GCC for cygwin,并且-std = …

c++ cin language-lawyer c++11

4
推荐指数
1
解决办法
269
查看次数

C ++参考指针作为更改指针的参数

我最近*&在函数中遇到了一个参数。
据我了解,它类似于**

为什么在更改指针的函数中需要它?例如,带有new关键字

假设我有一个指针int* a,如果我想a = new int;在该函数中进行操作,为什么还要将参数作为“指针引用”传递?

c++ pointers reference pass-by-reference

4
推荐指数
1
解决办法
125
查看次数

设置一个等于 {} 的向量;

以下代码始终有效还是依赖于编译器/平台?显然,我可以edges使用值构造函数进行初始化,但是我很好奇operator=edges初始化为大小 0时复制赋值是否在此处工作,然后设置为等于带括号的 r 值。

它适用于我的 macbook。

std::vector<std::vector<int>> edges;
edges = {{1,2,3},{4},{5,6}};
Run Code Online (Sandbox Code Playgroud)

c++ vector variable-assignment initializer-list c++11

4
推荐指数
1
解决办法
983
查看次数

部分模板模板向量特化

我有一个处理不同容器的通用函数。

template<template<class, class> class C, class T, class A>
void handle(C<T, A> const& c)
{
    cout << "General handling\n";
}
Run Code Online (Sandbox Code Playgroud)

现在,如果我将自定义容器传递给它,我希望它做出不同的反应。为简单起见,我首先尝试以单独的方式处理向量,尝试将此函数部分专门化为向量。
这就是我认为它应该是什么样子。

template<class T, class A>
void handle<std::vector>(std::vector<T, A> const& c)
{
    cout << "vector handling\n";
}
Run Code Online (Sandbox Code Playgroud)

但是gcc给出了以下错误:

无法执行程序编译器返回:1 编译器标准错误:16:36:错误:模板 id 'handle class std::vector>' in declaration of primary template 16 | (std::vector const& c) |

这可以通过部分模板特化来完成吗?

c++ templates partial-specialization c++14

4
推荐指数
1
解决办法
96
查看次数

结构变量成员后的花括号是什么意思?

在一些维护(Valgrind'ing)期间,我遇到了这个代码:

#pragma pack(push, 1)
struct somename
{
  uint16_t a{};
  uint16_t b{};
  uint32_t  c{};
};
#pragma pack(pop)
Run Code Online (Sandbox Code Playgroud)

我希望{}告诉编译器始终将值初始化为 0(使用 new 或使用堆栈变量分配时),但我找不到任何示例或文档。我在这个假设中正确吗?如果不:

{}struct 成员变量后的花括号是什么意思?

c++ initialization c++11

4
推荐指数
1
解决办法
208
查看次数