我知道这是一个非常基本的问题,但是我在用高级语言编写一些项目之后开始使用一些基本的C++编程.
基本上我有三个问题:
如何通过引用传递静态分配的数组?
void foo(int (&myArray)[100])
{
}
int main()
{
int a[100];
foo(a);
}
Run Code Online (Sandbox Code Playgroud)
是否(&myArray)[100]有任何意义或只是通过引用传递任何数组的语法?我不明白单独的括号后跟大括号.谢谢.
标准容器传播const。也就是说,如果容器本身是const,则它们的元素自动为const。例如:
const std::vector vec{3, 1, 4, 1, 5, 9, 2, 6};
ranges::fill(vec, 314); // impossible
const std::list lst{2, 7, 1, 8, 2, 8, 1, 8};
ranges::fill(lst, 272); // impossible
Run Code Online (Sandbox Code Playgroud)
内置数组还传播const:
const int arr[] {1, 4, 1, 4, 2, 1, 3, 5};
ranges::fill(arr, 141); // impossible
Run Code Online (Sandbox Code Playgroud)
但是,我注意到std::span(大概)不会传播const。最小的可重现示例:
#include <algorithm>
#include <cassert>
#include <span>
namespace ranges = std::ranges;
int main()
{
int arr[] {1, 7, 3, 2, 0, 5, 0, 8};
const std::span spn{arr};
ranges::fill(spn, 173); // this compiles
assert(ranges::count(arr, …Run Code Online (Sandbox Code Playgroud) 在这个站点上,他们给出了一个文字类的例子:
#include <iostream>
#include <stdexcept>
class conststr
{
const char* p;
std::size_t sz;
public:
template<std::size_t N>
constexpr conststr(const char(&a)[N]) : p(a), sz(N - 1) {}
constexpr char operator[](std::size_t n) const
{
return n < sz ? p[n] : throw std::out_of_range("");
}
constexpr std::size_t size() const { return sz; }
};
constexpr std::size_t countlower(conststr s, std::size_t n = 0,
std::size_t c = 0)
{
return n == s.size() ? c :
s[n] >= 'a' && s[n] <= 'z' ? …Run Code Online (Sandbox Code Playgroud) 我正在尝试更改数组中对象的参数,但是当我将它传递给函数时,它似乎正在创建一个新参数。
我已经看到了类似的问题和答案,像这一个,但它不为我工作,因为我没有在最后的代码固定的数组的大小。
我创建了一个非常简短的代码版本来显示问题。
#include <iostream>
using namespace std;
class Vect {
public:
Vect(int x, int y)
{
_x = x;
_y = y;
}
int _x;
int _y;
};
void ChangeX(Vect tests[], int size)
{
for (int i = 0; i < size; i++) {
tests[i]._x = 39;
}
}
int main()
{
Vect v1 = Vect(1,2);
Vect v2 = Vect(6,3);
cout << "Initial X: ";
cout << v1._x;
cout << "\n";
Vect vectors[2] = { v1, …Run Code Online (Sandbox Code Playgroud) 我试图在数组A中保存32位数字的二进制等价.为了测试我的showbits()函数,当我遇到这个东西时,我选择了8,9:
当我在函数showbits()中放置memset时,我在代码中面临一个不合理的事情,我正在考虑荒谬的整数,而我期望输出为
00000000000000000000000000001000
这是8的二进制等价物.当我将memset放在main()方法中时,它正常工作并给我正确的输出.我走出界限(我看不到它!).
我的代码: SHOWBITS:
void showbits(int A[32],int num)
{
int k=0;
memset(A,0,sizeof(A));
while(num>0)
{
A[k] = num&1;
k++;
num>>=1;
}
return ;
}
Run Code Online (Sandbox Code Playgroud)
注意:我已将memset放在showbits中,我得到的答案不正确! 主要:
int main()
{
int A[32],i;
showbits(A,8);
for(i=31;i>=0;i--)
printf("%d",A[i]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
整个测试程序:
#include<stdio.h>
#include<algorithm>
#include<cstring>
using namespace std;
void showbits(int A[32],int num)
{
int k=0;
memset(A,0,sizeof(A));
while(num>0)
{
A[k] = num&1;
k++;
num>>=1;
}
return ;
}
int main()
{
int A[32],i;
showbits(A,8);
for(i=31;i>=0;i--)
printf("%d",A[i]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我在showbits()之前将那个memset语句放在Main方法中时,我得到了正确的输出!
编辑 如果有人对我得到的东西感兴趣
398420075242008462686872420075219611920941961187434-2205336646196127610926869242 …
所以我有一个结构数组和一个使用该数组的类。构造函数需要引用作为常量的结构数组。我正在努力将结构数组作为引用传递,以便类构造函数可以使用它。由于结构数组不会改变,因此无需复制它,我只需要引用它。这是我当前生成错误的代码。
struct c_cs_pair_t
{
const wchar_t c;
const wchar_t* cs;
};
class Converter
{
protected:
size_t _length;
const c_cs_pair_t(& _pairs)[];
public:
Converter ( const c_cs_pair_t(& pairs)[] ) : _pairs(pairs) {
_length = sizeof ( pairs ) / sizeof( c_cs_pair_t );
}
};
const c_cs_pair_t c2cspairs[] =
{ { L'\n', L"\\n" }
, { L'\0', L"\\0" }
, { L'\\', L"\\\\" }
, { L'[', L"\\[" }
, { L']', L"\\]" }
, { L'.', L"\\." }
, { L':', L"\\:" …Run Code Online (Sandbox Code Playgroud) 如果数据类型是typedef,如何通过引用传递数组.我正在学习c ++,我阅读了call-by-reference的概念,但是当我按照那个实现时 - 我收到了一个错误(在代码之后粘贴在下面).请问,任何人都可以解释发送数组作为参考调用的最佳方式吗?
#include <iostream>
#include <vector>
using namespace std;
typedef unsigned long ulong;
ulong fib_dynamic(ulong n, ulong &memo[]){
if(n < 2) return 1;
if(memo[n] == 0){
memo[n] = fib_dynamic(n-1, memo) + fib_dynamic(n-2, memo);
}
return memo[n];
}
ulong fib_iterative(ulong n){
ulong fib[n+1];
fib[0] = 1;
fib[1] = 1;
for(int i=2; i<n; i++) {
fib[i] = fib[i-1] + fib[i-2];
}
return fib[n-1];
}
int main(){
ulong n;
cout << "Welcome to Fib Calculator\nEnter the n:";
cin >> n;
ulong …Run Code Online (Sandbox Code Playgroud)