我在玩距离循环,例如:
for (auto i : range) {
这相当于
auto it = range.begin();
auto i = *it;
但我想要的是:
for (auto it : range) {
*it; // the it var is not derefernced by range loop
有没有办法做到这一点?可能是这样的:
for (auto it : make_range(range)) {
*it; // the it var is not derefernced by range loop
……
到目前为止,我找到的一个解决方案是使用
我正在尝试编写一个c++程序,该程序会提示输入几个1个单词,直到输入一个标记值。一旦输入了这个值(即“完成”),程序就应该输出用户输入的所有单词。
我有一般的格式;但是,这不会存储多个值,如果是string...any帮助就太好了,谢谢。
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
string word;
word = "";
cout << "Enter the next bla bla now:
#include <map>
int main()
{
auto coll = std::map<int, int>{{1, 2}};
auto pos = coll.begin();
(*pos).first; // OK. Conforming to the C++ standard.
pos->first; // Does this conform to the C++ standard too?
}
根据,迭代器对象pos必须保证*pos和++pos是有效的表达式。然而,C++标准并不要求pos->f
我在大学刚开始学习C++,我的任务是完成以下工作:我必须编写一些代码,使用迭代(即循环)来计算整数数组中项的累积和;我的代码是:
int main() {
int myArray[] = {1,2,3,4,5};
int i;
int j;
j+= myArray[];
for(i=0;i<5;i++){
printf("%d\n",myArray[j]);
}
}
虽然这段代码没有产生我想要的东西,我也不知道下一步该做什么。
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
string TmpS;
cin >> TmpS;
stable_sort(TmpS[0], TmpS[TmpS.size()]);
} 在Compiler Explorer上编译这段代码会得到以下错误: In file included from /opt/compiler-explorer/gcc-11.1.0/include/c++/11.1.0/algorithm:62,
我对使用地图很陌生,当我尝试使用下面的代码对它进行排序时
#include <iostream>
#include<map>
#include<algorithm>
using namespace std;
int main()
{
map<int,int> mp;
mp[2]++;
mp[5]++;
mp[1]++;
sort(mp.begin(), mp.end());
}
引发一个错误:
In file included from /usr/include/c++/6/algorithm:62:0,
C++ 上最受欢迎的帖子声称,不清楚过去-结束迭代器(即,由end()、cend()、rend()和crend()返回的迭代器)是否根据与指向容器中元素的常规迭代器相同的规则无效。这些声明都是针对2003年和2011年的C++做出的,遵循了一篇讨论的文章,其中被接受的答案表明2003年的标准在这个问题上是模棱两可的。这个结论基于23.1/10 (在swap()的上下文中)中的一条评论,该评论似乎暗示,当规范没有明确提到过期迭代器的无效时,它们可能会失效。
对该帖子的问题(由mike-seymour提出)的评论表明,在deques的情况下,C++11在这个问题上是明确的。我的问题是关于所有容器: