/** module.h */
#pragma once
class A {
friend void helpers::logValue(const A &);
int _val;
public:
A() {}
};
namespace helpers {
static void logValue(const A &a) {
std::cout << a._val; // <== ERROR: '_val' is not accessible
}
}
Run Code Online (Sandbox Code Playgroud)
如何在另一个命名空间中声明友元函数?
使用 C++23 视图::join_with 时可以使用多字符串作为分隔符吗?
gcc 14 返回错误消息。
#include <format>
#include <stdio.h>
#include <string>
#include <algorithm>
#include <ranges>
using namespace std;
int main(){
const string a[2]={"6", "7"};
const string buffer1 = a | views::join_with('\n') | ranges::to<string>(); //OK
printf("%s", buffer1.c_str());
const string buffer2 = a | views::join_with(",\n") | ranges::to<string>(); //KO
printf("%s", buffer2.c_str());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
prog.cc: In function 'int main()':
prog.cc:13:30: error: no match for 'operator|' (operand types are 'const std::string [2]' {aka 'const std::__cxx11::basic_string<char> [2]'} and 'std::ranges::views::__adaptor::_Partial<std::ranges::views::_JoinWith, const char*>')
13 | …Run Code Online (Sandbox Code Playgroud) 嘿大家我是初学程序员,我的递归代码有问题来计算数字的阶乘.
我得到分段错误,我不知道为什么会这样.
任何帮助将非常感谢:)
(在我的代码中,我试图计算例如4的阶乘)
#include <stdio.h>
int factorial(int i) {
int result = i * factorial(i - 1);
return result;
}
int main()
{
int result = factorial(4);
printf("result is %d", result);
}
Run Code Online (Sandbox Code Playgroud) 这是我的代码,我期待输出,但我没有得到。它在接受输入后停止,如果我给出名字 Harsh,我期待输出
你的名字叫严酷
#include <iostream>
#include <cstring>
using namespace std;
int main() {
cout << "Enter your name" << endl;
char *s;
cin >> s;
cout << "Your name is " << s;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我也尝试过,cin.getline(s,100);但仍然不起作用。所以我请求你解决这个问题并给我解决方案。
问题陈述是:
给定一个有 n 个柱子和 k 种颜色的栅栏,找出绘制栅栏的方法数,以便不超过两个连续的柱子具有相同的颜色。由于答案可能很大,因此返回它模 10^9 + 7。
测试用例示例
n = 3 k = 2 输出:6
n = 2 k = 4 输出:16
我写的代码是
long long countWays(int n, int k){
if(n==1)
{
return k;
}
long long ans;
long long mod=1000000007;
long long same=k%mod;
long long diff=(k*(k-1))%mod;
for(int i=3;i<=n;i++)
{
long long temp=diff%mod;
diff=(((diff+same)%mod)*(k-1)%mod)%mod;
same=temp%mod;
}
return (same+diff)%mod;
}
Run Code Online (Sandbox Code Playgroud)
此代码的大部分部分都成功运行,但测试用例 n=27608 k=99118 失败,输出应该是 108753863,但输出却是 340817251。
我认为问题可能出在模 10^9 + 7 上。
我尝试了一切,但似乎没有任何效果。