我正在尝试学习 C++,但尽管花了很多时间寻找答案,但我还是无法理解这里的代码:
#include <iostream>
void printArray1(int (&array)[3]) {
for(int x : array)
std::cout << x << " ";
}
void printArray2(int array[]) {
for(int x : array) // compiler error, can't find begin and end
std::cout << x << " ";
}
int main() {
int a[3] = {34,12,88};
for(int x : a)
std::cout << x << " ";
std::cout << std::endl;
printArray1(a);
printArray2(a);
std::cout << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在printArray1 中,我们接收到一个参数,该参数是对大小为3 的数组的引用。这是否意味着我们正在接收整个数组的地址,还是只接收到大小为3 的数组中第一个元素的地址?另外,这个参数是如何传递给循环的?
在printArray2 中,我们接收到一个指向数组中第一个元素的指针,对吗?换句话说,我们也收到了一个地址,就像在 printArray1 中一样?因此,此函数中基于范围的 for 循环将无法编译,因为我们没有数组的大小数据,对吗? …
我有一个返回容器的函数。我们姑且称之为“ Container”吧。
Container GenerateRandomContainer() { ... }
Run Code Online (Sandbox Code Playgroud)
该函数将生成一个包含每次调用都不同的随机元素的容器。
当我使用 for every 循环迭代此容器时,如下所示:
for(Element e : GenerateRandomContainer()) { ... }
Run Code Online (Sandbox Code Playgroud)
它会生成一个新的Containereach迭代还是在进入foreach循环时只生成一个迭代?
我是 C++ 新手,为了练习,我一直在解决projecteuler.net上的一些问题。其中一个问题涉及分析一个 1000 位数字,因此我编写了一个程序,可以读取该数字并将其存储在一个向量中。为了测试我的代码,我已将向量打印到控制台。我很困惑,因为如果我使用手动指定的索引for循环,它会打印出正确的值,但是当我将 rangedfor与auto&关键字一起使用时,它会感到困惑并打印出错误的值。
这是我的代码:
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
vector<int8_t> reader(string datei)
{
ifstream file;
char inputChar;
vector<int8_t> data;
file.open(datei);
while(file >> inputChar)
data.push_back(inputChar);
file.close();
return data;
}
int main()
{
string a;
cout << "Select file to open:" << endl;
cin >> a;
vector<int8_t> numbers = reader(a);
for(int i = 0; i < numbers.size(); i++)
cout << numbers[i];
for(int i = 0; i < …Run Code Online (Sandbox Code Playgroud) 我正在研究面向C++面向对象的类中的向量,并且我们已经介绍了基于范围的for循环的概念.我决定分别练习基于for循环的范围,这样我就可以习惯语法,但是我遇到了一个奇怪的问题.
#include<iostream>
using namespace std;
int main()
{
int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
for ( auto i: a)
{
cout << a[i] << " ";
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我运行上面的代码时,我的输出如下.
2 3 4 5 6 7 8 9 0 1按任意键继续......
我的输出应该是读
1 2 3 4 5 6 7 8 9 0按任意键继续......
任何人都可以告诉我为什么我的第一个索引被跳过?我有视觉工作室2013专业.
我收到错误
将'const double'绑定到类型'double&'丢弃限定符的引用
编译时:
g++ -std=c++11 main.cpp
main.cpp: In function ‘Point square(const Point&)’:
main.cpp:14:28: error: binding ‘const double’ to reference of type ‘double&’ discards qualifiers
for(double &a:{Q.x,Q.y,Q.z})
^
Run Code Online (Sandbox Code Playgroud)
虽然在线有关于此错误的其他问题,但我正在寻找这个特定代码的解决方案.我坚持使用ranged for.
#include <iostream>
#include <vector>
class Point
{
public:
double x,y,z;
};
Point square(const Point &P)
{
Point Q=P;
for(double &a:{Q.x,Q.y,Q.z})
a*=a;
return Q;
}
int main()
{
Point P{0.1,1.0,10.0};
Point Q=square(P);
std::cout<<"----------------"<<std::endl;
std::cout<<"Q.x: "<<Q.x<<std::endl;
std::cout<<"Q.y: "<<Q.y<<std::endl;
std::cout<<"Q.z: "<<Q.z<<std::endl;
std::cout<<"----------------"<<std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 假设我有以下简单程序(http://cpp.sh/5sygh):
#include <map>
#include <iostream>
using Key = std::pair<unsigned long, unsigned long long>;
struct KeyLess {
bool operator()(const Key& lhs, const Key& rhs) {
if (lhs.first < rhs.first) {
return true;
}
if (lhs.second < rhs.second) {
return true;
}
return false;
}
};
int main() {
std::map< Key , int, KeyLess> m;
m[Key{2, 169}] = 1;
m[Key{1, 255}] = 2;
m[Key{1, 391}] = 3;
m[Key{1, 475}] = 4;
std::cout << "Elements in map: " << m.size() << …Run Code Online (Sandbox Code Playgroud) 这个用向量元素的地址填充指针向量的代码v工作正常:
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> v{ 1,2,3,4,5 };
vector<int*> pv;
for (unsigned int i = 0; i < size(v); i++)
pv.push_back(&(v[i]));
for (auto i : pv)
cout << i << " ";
cout << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如何使用基于范围的for循环获得相同的结果.我尝试过这样的代码,但它不能正确填充指针的向量:
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> v{ 1,2,3,4,5 };
vector<int*> pv;
for (auto i : v)
pv.push_back(&i);
for (auto i : pv)
cout << i << " …Run Code Online (Sandbox Code Playgroud) 我试图auto &在ranged for循环中使用来修改值.但是我感到非常震惊,以至于它"无所不及".我附上下面的原始代码.它只是在矩阵中找到元素0,并将相应的行和列设置为全零.
#include<iostream>
#include<vector>
using namespace std;
void zerolify(vector<vector<int>>& m)
{
int row=m.size();
int col=col>0?m[0].size():0;
vector<int> r(row,0);
vector<int> c(col,0);
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
if(m[i][j]==0)
{
r[i]=1;
c[j]=1;
}
}
}
for(int i=0;i<row;i++)
{
if(r[i])
{
//cout<<"row: "<<i<<endl;
for(auto &e : m[i])
{
e=0;
}
//for(int j=0;j<col;j++)
//{
// m[i][j]=0;
//}
}
}
for(int i=0;i<col;i++)
{
if(c[i])
{
for(int j=0;j<row;j++)
{
m[j][i]=0;
}
}
}
}
void printMatrix(vector<vector<int>> m)
{
for(auto i:m)
{
for(auto j:i)
{ …Run Code Online (Sandbox Code Playgroud) c++ ×8
ranged-loops ×8
c++11 ×4
arrays ×2
for-loop ×2
auto ×1
containers ×1
dictionary ×1
foreach ×1
stdmap ×1