我编写了以下代码片段,将两种状态的字符串 ("+++--+-"或"yynnny") 转换为std::bitset:
#include <bitset>
#include <cstddef>
#include <string>
#include <iostream>
std::bitset<70> convertTwoStateString(std::string twoState)
{
std::bitset<70> a{0b0};
std::bitset<70> eins{0b1};
for(const auto c : twoState) {
if(c == '+') {
a <<= 1;
a |= eins;
}
if(c == '-') {
a <<= 1;
}
}
return a;
}
int main()
{
std::string s{"-+--+++--+--+"};
std::bitset<70> set = convertTwoStateString(s);
std::cout << set << std::endl;
//0000000000000000000000000000000000000000000000000000000000100111001001
}
Run Code Online (Sandbox Code Playgroud)
是否有更算法和/或更优雅的方法来进行此类转换?
我不确定我是否正确理解了堆栈。对于复数 a 和 b(a=3+5i 和 b=2+i),我有以下运算符重载。
struct complex{
int x;
int y;
};
complex& operator+=(complex& a, const complex b){
a.x=a.x+b.x;
a.y=a.y+b.y;
return a;
}
Run Code Online (Sandbox Code Playgroud)
现在我想知道返回值的引用指向哪里。
我认为在主堆栈帧中有一个用于 a = ax 和 64 位 ay 的内存区域,因为 ax/ay 是 int 类型。而operator+=stack-frame中的返回值a指向这个“a”-内存区域。
我想知道“a”-memory-area 是什么样子的,复杂类型的对象是如何存储在主堆栈框架中的?
它是像一个数组,引用指向“a[0]”还是 ax 和 ay 分开,你需要“两个”引用指针来指向复杂类型的对象。
我尝试为 <<-operator-overloading 编写一个简单的示例。以前,我从未使用过“朋友”这个关键词。但如果没有它,它就无法工作。我犯了什么错误或者为什么我需要这里的friend关键字?
class rational{
public:
rational(double num, double count)
: n(num),c(count){}
rational& operator+=(const rational& b){
this->n+= b.n;
this->c+= b.c;
return *this;
}
friend std::ostream& operator<<(std::ostream& os, const rational& obj)
{
std::cout << obj.n << "," << obj.c << std::endl;
return os;
}
private:
double n;
double c;
};
Run Code Online (Sandbox Code Playgroud)
谢谢
您知道该错误消息的含义以及我现在应该做什么吗?
vpn-global-dhcp3-252:build Dolyn$ make
Scanning dependencies of target mdatom
[ 10%] Building CXX object CMakeFiles/mdatom.dir/src/main.cpp.o
/Users/Dolyn/sources/src/main.cpp:124:11: fatal error:
'chrono' file not found
#include <chrono>
For high resolutio timings
^
1 error generated.
Run Code Online (Sandbox Code Playgroud)
我不知道我现在必须做什么。
我还是很困惑。如果我在 OpenMP 中使用 reduce 子句会发生错误共享吗?(两个代码片段都给出了正确的结果。)
一个小例子,其中需要数组的最大值:
double max_red(double *A, int N){
double mx = std::numeric_limits<double>::min();
#pragma omp parallel for reduction(max:mx)
for(int i=0; i<N; ++i){
if(A[i]>mx) mx = A[i];
}
return mx;
}
Run Code Online (Sandbox Code Playgroud)
这个例子也可以用额外的填充来编写
double max_padd(double *A, int N){
omp_set_num_threads(NUM_THREADS);
double local_max[NUM_THREADS][8];
double res;
#pragma omp parallel
{
int id = omp_get_thread_num();
local_max[id][0] = std::numeric_limits<double>::min();
#pragma omp for
for(int i=0; i<N; ++i){
if(A[i]>local_max[id][0])local_max[id][0]=A[i];
}
#pragma omp single
{
res = local_max[0][0];
for(int i=0; i<NUM_THREADS; ++i){
if(local_max[i][0]> res)res = local_max[i][0];
}
} …Run Code Online (Sandbox Code Playgroud) 我有一串球。每个球上都有一个箭头。
我想用绘图来表示上面的图形,这样我就可以旋转它,但我完全迷失了。如何将箭头定位在球上?
我开始制作前 4 个箭头,如下所示:
import plotly.graph_objs as go
import plotly
plotly.offline.init_notebook_mode()
x = [10.1219, 10.42579, 15.21396, 15.42468, 20.29639,20.46268, 25.36298, 25.49156]
y = [5.0545, 5.180104, 5.0545, 5.20337, 5.0545, 5.194271, 5.0545, 5.231627]
z = [5.2713, 5.231409, 5.2713, 5.231409, 5.2713 , 5.235852, 5.2713, 5.231627]
pairs = [(0,1), (2,3),(4,5), (6,7)]
trace1 = go.Scatter3d(
x=x,
y=y,
z=z,
mode='markers',
name='markers'
)
x_lines = list()
y_lines = list()
z_lines = list()
for p in pairs:
for i in range(2):
x_lines.append(x[p[i]])
y_lines.append(y[p[i]])
z_lines.append(z[p[i]])
x_lines.append(None)
y_lines.append(None)
z_lines.append(None)
trace2 …Run Code Online (Sandbox Code Playgroud) 我是C++的初学者,并且使用unix.所以这是我的问题.
我在main-function中写了几行,我需要一个在c_lib库中定义的函数.
main.cpp中:
#include <iostream>
#include "c_lib.cpp"
int main()
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想在终端上执行它,所以我写道
g++ -c c_lib.cpp
g++ -c main.cpp
g++ -o run c_lib.o main.o
Run Code Online (Sandbox Code Playgroud)
在此之前,没有错误报告.
然后
./run
Run Code Online (Sandbox Code Playgroud)
我收到了错误
错误:./ run:没有这样的文件或目录
怎么了?
在Matlab中,我想计算一个数组的sin(v)v = [0.1,0.01,0.001].但是有一个错误,因为令人费解的是,sin函数想要带有昏迷的浮点数:
>> sin(1.2)
Subscript indices must either be real positive integers or logicals.
>> sin(1,2)
ans =
0.93204
Run Code Online (Sandbox Code Playgroud)
为什么?这里发生了什么?因为cos的工作原理应该如此.
>> cos(1.2)
ans =
0.36236
>> cos(1,2)
Error using cos
Too many input arguments.
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助!
我必须编写一个程序,其中使用动态数组:
int size=1;
double* dyn_arr = new double[size];
int n=0;
double sum=0.0;
while(std::cin >> dyn_arr[n]){
sum = sum + dyn_arr[n++];
if(n==size){
size*=2;
double* new_array = new double[size];
std::copy(dyn_arr, dyn_arr + n, new_array);
delete[] dyn_arr;
dyn_arr = new_array;
}
}
Run Code Online (Sandbox Code Playgroud)
dyn_arr = new_array后面的部分看不懂delete[] dyn_arr了
dyn_arr是数组第一个元素的指针dyn_arr,不是吗?如何删除指针/数组并再次写入dyn_arr = new_array?
我写了这个简单的测试程序.但我无法理解这里发生了什么.因为输出中有一个奇怪的东西:
std::list<std::pair<double,double>> l;
l.push_back({0.3,0.9});
l.push_back({-0.3,0.5});
l.push_back({0.3,0.7});
l.push_back({1.2,1.83});
for(auto it=l.begin(); it!=l.end(); ++it){
double lx= it->first + 0.01;
double ly= it->second + 0.01;
it->first = lx;
it->second = ly;
if(lx < 0.0 || lx > 1.0 || ly < 0.0 || ly > 1.0){
it = l.erase(it);
}
Run Code Online (Sandbox Code Playgroud)
如果我打印列表,我得到:
0.32, 0.92
0.31, 0.71
Run Code Online (Sandbox Code Playgroud)
为什么迭代器会回到第一个元素(两次+0.1)?
我有这个unsigned int功能(这是任务的给定功能)
// read data from virtual memory
unsigned int read(const int index) {
return speicher[index];
}
Run Code Online (Sandbox Code Playgroud)
Inspeicher[index]是一个数字,我必须在 4 个分量中对其进行解码。第一个分量是 的值speicher[index] mod 16,第二个分量是(speicher[index])/(16*16)mod16我认为的值,依此类推。
带有错误消息的另一个示例。也许我无法理解该函数的作用:
对我来说,函数read(const int index)应该返回一个无符号整数,我可以像程序中的“普通”其他无符号整数一样使用它。
unsigned int index = 0;
unsigned int test = 1;
do {
unsigned int hexzahl = read(index);
test = read(index);
index = index + 1;
} while (index <= 255 && read(index) != 0);
// 37 actually i want, that the program breaks …Run Code Online (Sandbox Code Playgroud) 对于学校我必须下载CMake.现在我在mash-project上找到了一个快速入门指南,但我没有得到它.
CMAKE_BUILD_TYPE,CMAKE_OSX_ARCHITECTURES......谢谢你千万时间的帮助!
我有一个简单的功能,我需要划分两个整数.但是铸造不起作用.我无法理解我的代码中出了什么问题:
double new=0.0;
if(N>0) new = double(Ns)/double(N);
Run Code Online (Sandbox Code Playgroud)
这个地方的错误信息double new;是(错误:预期的unqualified-id)和地点new=double(Ns)/double(N)
以及