鉴于这些声明:
int a[3] {10,20,30};
std::tuple<int,int,int> b {11,22,33};
Run Code Online (Sandbox Code Playgroud)
我可以使用结构化绑定声明来解码a和b:
auto [x1,y1,z1] = a;
auto [x2,y2,z2] = b;
Run Code Online (Sandbox Code Playgroud)
但是,如果x1,y1等已经存在了,我该怎么办?
std::tie(x1,y1,z1) = a; // ERROR
std::tie(x2,y2,z2) = b; // OK
Run Code Online (Sandbox Code Playgroud)
这适用b但不适用a.是否有一个类似的简单构造,对工作a,还是我去取a[0],a[1]并a[2]分别?
我是JPEG新手,我正在尝试解码(可能已损坏的)JPEG(或更确切地说是JFIF)文件。
我的图像查看器程序抱怨有一个非法的0xb9标记。
该文件没有SOF(帧开始)标记,而是具有此APP1段
ff e1 00 0b 50 49 43 00 02 28 3c 01 00
Run Code Online (Sandbox Code Playgroud)
随后是带有0xb9标记的该段:
ff b9 00 11 08 06 4c 04 d3 03 01 22 00 02 11 01 03 11 01
Run Code Online (Sandbox Code Playgroud)
我在这里看什么
编辑
我被问到文件的来源。这是故事:
大约20年前,我通过CD-ROM购买了一些杂志。但是,只能在旧的Windows计算机上阅读这些杂志,因此我正在尝试寻找另一种阅读方法-最好是在我的Linux计算机上可以使用的方法。据我所知,杂志页面存储为许多JFIF文件,这些文件只是串联在一起。
因此,首先,我提取了一个JFIF文件,现在我试图找到一种查看它的方法。
编辑2
我被要求分享一个图片文件。我不确定是否要侵犯版权,因此几天后我将再次删除该文件。无论如何,可以从此处下载有问题的图片文件之一:
https://www.dropbox.com/s/9da72gdri8c9xwp/f1000.jpg
我不知道文件包含什么,除了它可能是来自MAD Magazine的页面。
该ff b9段看起来非常像SOF0段,将其更改为ff c0(SOF0)可使图片可见,但仅作为随机像素的集合。
上面提到的APP1段包含字符串PIC,它不是我知道的任何APP1段类型的一部分。
编辑3
既然已经回答了这个问题,为了避免任何版权问题,我将从Dropbox中删除该文件。感谢所有对此做出贡献的人。
考虑以下代码:
#include <iostream>
void f(int&& i)
{
std::cout << "f(int&&)\n";
}
void f(const int&& i)
{
std::cout << "f(const int&&)\n";
}
int fun_i()
{
return 0;
}
const int fun_ci()
{
return 0;
}
int main()
{
f(fun_i());
f(fun_ci());
}
Run Code Online (Sandbox Code Playgroud)
如果我用MSVC 2012编译它,输出是:
f(int&&)
f(const int&&)
Run Code Online (Sandbox Code Playgroud)
如果我用GCC 4.7编译,输出是:
f(int&&)
f(int&&)
Run Code Online (Sandbox Code Playgroud)
哪个是对的?
(如果我删除f的第二个定义,程序将无法在MSVC 2012下编译,但它确实在GCC 4.7下编译.)
这个C++ 11代码对我来说很好:
#include <iostream>
#include <vector>
#include <array>
using namespace std;
struct str {
int first, last;
};
vector<str> fields {
{1,2}, {3,4}, {5,6}
};
int main()
{
for (str s : fields)
cout << s.first << " " << s.last << endl;
}
Run Code Online (Sandbox Code Playgroud)
它打印出六个预期值.
但是,如果我vector<str>改为array<str,3>,gcc会给我这个错误:"std :: array'的初始化程序太多了".
如果我改变了初始化fields:
array<str,3> fields {
str{1,2}, str{3,4}, str{5,6}
};
Run Code Online (Sandbox Code Playgroud)
事情很顺利.
那么为什么我str{1,2}在使用时需要std::array,但仅{1,2}在使用时std::vector?
请考虑以下代码:
#include <iostream>
#include <functional>
using namespace std;
template<class T>
void fun(T t)
{
t+=8;
}
int main()
{
int i = 0;
fun(ref(i));
cout << i << endl;
}
Run Code Online (Sandbox Code Playgroud)
此代码打印"8".我假设fun()中的t自动转换为int&.
但是,如果我取代t+=8用t=8,程序将无法编译.
为什么?
C++大师Herb Sutter建议我们几乎总是使用"auto".他在他的网站上说明了这一点,他最近在CppCon 2014大会上重申了这一点.
我试图听从他的建议而且我不相信.这里是否有人同意Sutter,谁可以告诉我原因
auto gender = string{};
auto number = string{};
auto person = string{};
Run Code Online (Sandbox Code Playgroud)
比...更好
string gender, number, person;
Run Code Online (Sandbox Code Playgroud)
这是我最终写在我的代码中,因为我无法忍受汽车风格.
编辑:
auto gender = string{}, number = string{}, person = string{};
Run Code Online (Sandbox Code Playgroud)
也是一种可能性,但在我看来,情况更糟.
从JavaScript程序中,我希望能够判断Bootstrap是否将当前显示大小视为xs,sm,md或lg.
这有效:
在HTML中写这个:
<p class="visible-xs-block xyzzy" data-size="xs"></p>
<p class="visible-sm-block xyzzy" data-size="sm"></p>
<p class="visible-md-block xyzzy" data-size="md"></p>
<p class="visible-lg-block xyzzy" data-size="lg"></p>
Run Code Online (Sandbox Code Playgroud)
然后在JavaScript(用JQuery)中,这个表达式
$('.xyzzy:visible').attr('data-size')
Run Code Online (Sandbox Code Playgroud)
返回xs,sm,md或lg.
这有效,但在我看来,这是一种相当笨拙的方式.
有更简单的方法吗?
我有两个类A,B其中B是 的子类A。我需要两个类才能使用std::enable_shared_from_this。
我试过这个:
#include <memory>
#include <iostream>
#include <vector>
class A : public std::enable_shared_from_this<A> {
public:
void insertme(std::vector<std::shared_ptr<A>>& v) {
std::cout << "A::insertme\n";
v.push_back(shared_from_this());
std::cout << "OK\n";
}
};
class B : public A, public std::enable_shared_from_this<B> {
public:
void insertme(std::vector<std::shared_ptr<B>>& v) {
std::cout << "B::insertme\n";
v.push_back(std::enable_shared_from_this<B>::shared_from_this());
std::cout << "OK\n";
}
};
int main()
{
std::vector<std::shared_ptr<A>> va;
std::vector<std::shared_ptr<B>> vb;
std::shared_ptr<A> pa = std::make_shared<A>();
std::shared_ptr<B> pb = std::make_shared<B>();
pa->insertme(va);
pb->insertme(vb);
}
Run Code Online (Sandbox Code Playgroud)
(为了避免 …
c++ multiple-inheritance shared-ptr weak-ptr enable-shared-from-this
C++11 中动态内存的分配和释放是线程安全的吗?换句话说:线程可以像单线程代码一样自由地使用newand吗?delete
在TypeScript中,如果使用定义函数() => {...},则this变量将引用周围类的实例.但是如果你使用function () {...},this变量将具有旧的JavaScript解释.
有没有办法this在TypeScript函数中访问这两个变量?
在TypeScript中使用JQuery时偶尔需要这个:
class X {
private v : string;
constructor() {
$('.xyz').on('change', function() {
this.v = $(this).prop('value'); // Two different this's
})
}
}
Run Code Online (Sandbox Code Playgroud)
在代码的中心行中,第一个this应该引用类X对象,而第二个this应该引用触发事件的JQuery对象.
c++ ×6
c++11 ×5
jquery ×2
arrays ×1
auto ×1
c++17 ×1
javascript ×1
jpeg ×1
shared-ptr ×1
stdvector ×1
this ×1
typescript ×1
weak-ptr ×1