假设有一个这样的循环:
\n\nfor(size_t i=0, n=ar.size(); i<n; ++i)\n{\n // ...\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n是否可以将其重写为:
\n\nfor(auto i=0, n=ar.size(); i<n; ++i)\n{\n // ...\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n换句话说,这两个变量将i
始终n
是相同的数据类型。
当我尝试这样的事情时:
\n\nauto i=0, s="";\n
Run Code Online (Sandbox Code Playgroud)\n\ng++ 4.8.4 生成错误inconsistent deduction for \xe2\x80\x98auto\xe2\x80\x99: \xe2\x80\x98int\xe2\x80\x99 and then \xe2\x80\x98const char*\xe2\x80\x99
。但我无法确定它是否只是 g++,或者根据标准实际上需要使用类型推导中的每个值。
有没有办法允许concept
使用模板参数,可以使用提供的任何模板参数?
即模板参数占位符的某种通配符魔法?
\n一个使用示例:
\ntemplate<class Me, class TestAgainst>\nconcept derived_from_or_same_as = \n std::same_as<Me, TestAgainst> ||\n std::derived_from<Me, TestAgainst>;\n
Run Code Online (Sandbox Code Playgroud)\n需要上面的内容是因为不幸的是,原始类型的 行为与和 的类类型不同。is_base_of
derived_from
现在我们可以定义一个Pair concept
来检查提供的类型:
template<class P, class First, class Second>\nconcept Pair = requires(P p) {\n requires derived_from_or_same_as<decltype(p.first), First>;\n requires derived_from_or_same_as<decltype(p.second), Second>;\n};\n
Run Code Online (Sandbox Code Playgroud)\n用例 [a] - 接受任何有效的As或As子类型对:
\n// this works well\nvoid doWithPairOfA(const Pair<A, A> auto& p) { /* */ }\n
Run Code Online (Sandbox Code Playgroud)\n用例 …
我编写了以下测试:
#include <cassert>
#include <iostream>
#include <string>
#include <cmath>
#include <functional>
// Works with std::function !
//std::function<double(double)> set_unary_operation(const std::string& unary)
auto set_unary_operation(const std::string& unary)
{
if(unary == "EXP")
return [](const double& x){ return std::exp(x);};
if(unary == "LOG")
return [](const double& x){ return std::log10(x);};
if(unary == "_LN")
return [](const double& x){ return std::log(x);};
if(unary == "ABS")
return [](const double& x){ return std::abs(x);};
return [](const double& x){return x;};
}
bool is_equal(double&& value, double&& test)
{
double epsilon = 0.0000001;
return (value-epsilon < …
Run Code Online (Sandbox Code Playgroud) 在 C++ 中,auto
关键字强制编译器在编译时推断变量的类型。所以在这个例子中
#include <vector>
int main()
{
std::vector<int> my_vec = {1, 2, 3};
auto my_vec_it = my_vec.begin();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译器会推断出my_vec_it
具有 type std::vector<int>::iterator
。我知道这是因为更换auto
用std::vector<int>::iterator
和重新编译产生完全相同的可执行文件。
我发现auto
在编译器不同意我认为变量声明应该是什么的情况下很方便,我只是希望它停止抱怨以便我可以继续写作。换句话说,auto
当我不太了解我正在编写的代码时,我会使用它,这似乎是一个坏习惯。如果编译器和我在变量的类型上存在分歧,这种分歧可能会渗透并在我的代码中进一步导致更复杂、根深蒂固的错误。这让我想知道auto
真正的用途是什么。
auto
我上面描述的使用是一种不好的编程习惯,如果是的话,它的一些更原则性的用途是什么?
我在 C++ 中练习 lambda 函数,下面的代码工作正常
void insertionSort(int* a, int size, bool reverse=false) {
auto comp = [](int a, int b, bool reverse) {
return reverse ? a > b : b < a;
};
for (int i = 0; i < size; i++) {
int current = a[i];
cout << current <<endl;
int j = i-1;
while (j >= 0 && comp(current, a[j], reverse)) {
a[j+1] = a[j]; //shift right
j--;
}
a[j+1] = current;
}
show(a, size); //another function …
Run Code Online (Sandbox Code Playgroud) 有没有办法设置(扩展等...)Visual Studio 代码,以便在保存我正在处理的文件后自动刷新浏览器(Firefox Dev、Chrome Dev)?(如 Live Server 扩展)
我正在本地安装 Apache(是的,我正在使用 WordPress :)
例如,我创建了一个新的 create-react-app 并从 index.js 中删除了 App 导入。ESLint 显示了该问题,我可以通过单击“App”并按“Ctrl + Space”或“Ctrl +”来修复它。并单击导入应用程序,但我希望在保存时导入文件。
源操作 > 添加所有缺失的导入不起作用,但有趣的是组织导入却可以。
我不知道可能是什么问题。我想使用组织导入并在保存时添加丢失的导入。
我的设置.json
+ 我意识到 VS Code 根本无法判断缺少哪些文件,因为当我删除自动导入扩展时,我意识到 ctrl + space 和 ctrl + 的一件事。停止工作。
{
"workbench.iconTheme": "vscode-icons",
"liveServer.settings.donotShowInfoMsg": true,
"window.zoomLevel": 1,
"git.enableSmartCommit": true,
"eslint.format.enable": true,
"editor.formatOnSave": true,
"javascript.updateImportsOnFileMove.enabled": "always",
// "files.autoSave": "afterDelay",
"diffEditor.renderSideBySide": false,
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"explorer.confirmDragAndDrop": false,
"emmet.triggerExpansionOnTab": true,
"emmet.includeLanguages": {
"javascript": "javascriptreact"
},
"emmet.syntaxProfiles": {
"javascript": "jsx"
},
"prettier.arrowParens": "always",
"editor.fontLigatures": true,
"workbench.activityBar.visible": true,
"workbench.preferredDarkColorTheme": "One Dark Pro",
"oneDarkPro.bold": true,
"oneDarkPro.editorTheme": …
Run Code Online (Sandbox Code Playgroud) auto
在for
循环的情况下,C++ 是如何推断数据类型的——从初始化还是从条件?
long long n;
cin>>n;
for(auto i=1; i<=n; i++)
cout << ((i * i) * ((i * i) - 1)) / 2 - 2 * (2 * (i - 1) * (i - 2)) << "\n";
Run Code Online (Sandbox Code Playgroud)
这里,将i
是一个整数long long
?我的代码失败(可能是由于溢出-输出负值,N = 10000),当我用auto
,当我使用过long long
。
#include <vector>
#include <iostream>
using namespace std;
int main(void)
{
vector<int> a = {1, 2, 3, 4, 5};
for (auto &x : a)
cout << x << endl;
}
Run Code Online (Sandbox Code Playgroud)
#include <vector>
#include <iostream>
using namespace std;
int main(void)
{
vector<int> a = {1, 2, 3, 4, 5};
for (auto x : a)
cout << x << endl;
}
Run Code Online (Sandbox Code Playgroud)
上面的两个代码打印相同的值(1、2、3、4、5)。但是初始化 &x 和 x 之间有什么不同吗?谢谢阅读!
当编写具有返回类型的函数时,auto
我们可以使用constexpr if
返回不同的类型。
auto myfunc()
{
constexpr if (someBool)
{
type1 first = something;
return first;
}
else
{
type2 second = somethingElse;
return second;
}
}
Run Code Online (Sandbox Code Playgroud)
然而,我正在努力弄清楚如何仅将其中一种类型作为参考。看来以下代码仍然返回两个分支的右值
auto myfunc()
{
constexpr if (someBool)
{
type1 &first = refToSomething;
return first;
}
else
{
type2 second = somethingElse;
return second;
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点?谷歌并没有透露太多,因为有很多关于自动和引用返回的更一般使用的教程。在我的特定情况下,该函数是一个类方法,我想返回对成员变量的引用或数组的视图。