我有一个存储数组的类,我需要编写一个方法来返回指向该数组的指针,以便其他对象可以访问/修改它.
在我的程序的旧版本中,我通过使用C风格定义数组来实现这一点.即,拥有一个私有元素bool* list,然后在构造函数中分配内存(并在析构函数中释放它).那么方法非常简单:
bool* MyClass::getList() {
return list;
}
Run Code Online (Sandbox Code Playgroud)
现在,我决定重写代码并使用std::vector<bool>而不是经典数组.问题是我还修改了上面的方法:
bool* MyClass::getList() {
return &(list[0]);
}
Run Code Online (Sandbox Code Playgroud)
这似乎是将C++向量转换为C数组的标准方法.但是,我无法编译我的代码,我收到以下错误:
error: taking address of temporary [-fpermissive]
error: cannot convert ‘std::vector<bool>::reference* {aka std::_Bit_reference*}’ to ‘bool*’ in return
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮我解决这个问题并告诉我该怎么办?
(我还读过另一个选择是使用list.data()指针,但这只适用于最新版本的C++编译器.我不确定这是不是一个好主意).
谢谢,
考虑以下装饰器,它将任何二元运算符扩展到多个参数:
from typing import Callable, TypeVar
from functools import reduce, wraps
T = TypeVar('T')
def extend(binop: Callable[[T, T], T]):
""" Extend a binary operator to multiple arguments """
@wraps(binop)
def extended(*args: T) -> T:
if not args:
raise TypeError("At least one argument must be given")
return reduce(binop, args)
return extended
Run Code Online (Sandbox Code Playgroud)
然后可以按如下方式使用:
@extend
def fadd(x: float, y: float) -> float:
""" Add float numbers """
return x + y
@extend
def imul(x: int, y: int) -> int:
""" Multiply integers """ …Run Code Online (Sandbox Code Playgroud) 我写一个代码来解决以下问题:给定一组数字x[0],x[1],... x[N-1],发现,使得他们在升序排序的置换.在换句话说,我想找到一个置换{0,2,...,N-1}如i[0],i[1]......,i[N-1]这样x[i[0]] <= x[i[1]] <= ... <= x[i[N-1]].
为此,我将x向量和索引向量i(最初填充i[j] = j)存储为类的私有成员.我还定义了一个私有方法
bool MyClass::compare(size_t s, size_t t) {
return (x[s] < x[t]);
}
Run Code Online (Sandbox Code Playgroud)
现在,我打电话std::sort如下
std::sort(i.begin(), i.end(), compare);
Run Code Online (Sandbox Code Playgroud)
我期望得到理想的结果.但是代码没有编译,我收到以下错误:
error: no matching function for call to ‘sort(std::vector<long unsigned int>::iterator, std::vector<long unsigned int>::iterator, <unresolved overloaded function type>)’
Run Code Online (Sandbox Code Playgroud)
我必须正确完成所有事情,以及std::sort提及我可以将函数作为比较运算符传递给std::sort(http://www.cplusplus.com/reference/algorithm/sort/)的文档
感谢所有的帮助提前.
我在一个非常简单的代码上得到了一个非常奇怪的错误,我无法修复.
我已经定义了以下函数对象:
template<const size_t n> class L2Norm {
public:
double operator()(const Point<n>& p) {
/* computes the L2-norm of the point P ... */
}
double operator()(const Point<n>& p,
const Point<n>& q) {
return L2Norm<n>(p-q);
}
};
Run Code Online (Sandbox Code Playgroud)
这里的类Point<n>很好地定义了存储n点在一n维空间中的坐标(带有所需的运算符,......).
我希望得到一个点的l2范数p(Point<5> p例如创建)L2Norm<5>(p).但这给了我以下错误:
no matching function for call to ‘L2Norm<5ul>::L2Norm(Point<5ul>&)’
note: candidates are: L2Norm<n>::L2Norm() [with long unsigned int n = 5ul]
note: candidate expects 0 arguments, 1 provided
note: L2Norm<5ul>::L2Norm(const L2Norm<5ul>&) …Run Code Online (Sandbox Code Playgroud) std::array当初始数组值是构造函数的参数时,我想知道在构造函数中初始化类成员的正确方法是什么?
更具体地说,请考虑以下示例:
class Car {
public:
Car(const std::string& color, int age): color_(color), age_(age) {}
// ...
private:
std::string color_;
int age_;
};
class ThreeIdenticalCars {
private:
std::array<Car, 3> list;
public:
ThreeIdenticalCars(const std::string& color, int age):
// What to put here to initialize list to 3 identical Car(color,age) objects?
{}
};
Run Code Online (Sandbox Code Playgroud)
显然,一种方法是写作list({Car(color,age), Car(color,age), Car(color,age)}),但如果我们想要30辆相同的汽车而不是3辆,这显然无法扩展.
如果不是std::array我使用std::vector的解决方案list(3, Car(color,age)(或者list(30, Car(color, age))在我的问题中,列表的大小已知,我认为使用更正确)std:array.
我有以下非常基本的问题.我想使用stl迭代器而不是传统的C类型指针来填充函数中的数组.通过C风格的方式我的意思是以下示例:
void f(double* v, size_t n) {
for (int i = 0; i < n; i++)
v[i] = 10; /* a more reasonable value in practice! */
}
Run Code Online (Sandbox Code Playgroud)
我会使用迭代器将其转换为C++样式,如下所示:
void f(vector<double>::const_iterator first, vector<double>::const_iterator last) {
for(vector<double>::iterator it = first; it != last; it++)
*it = 10;
}
Run Code Online (Sandbox Code Playgroud)
但我得到编译错误.如果我用iterator而不是const_iterator问题就会解决.但是,我想知道这是否正确?因为我认为vector.begin()和vector.end()迭代器是不变的.
提前致谢!
考虑以下代码:
#include <iostream>
using namespace std;
class A {
private:
int x;
public:
int& get_ref() {
cerr << "non const" << endl;
return x;
}
const int& get_ref() const {
cerr << "const" << endl;
return x;
}
};
int main () {
A a;
a.get_ref() = 10;
cout << a.get_ref() << endl;
const int& y = a.get_ref();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我希望第二次和第三次调用a.get_ref()运行第二版get_ref()方法(并输出const标准错误).但看起来总是第一个版本被调用.如何实现两个不同的'getter'并确保根据上下文调用正确的版本?即,至少在第三次通话时
const int& y = a.get_ref();
Run Code Online (Sandbox Code Playgroud)
第二个版本执行?(一个不优雅的解决方案是使用不同的名称,例如get_ref,get_const_ref但我试图看看是否可以避免.)
我刚刚开始学习Perl,因此我的问题可能看起来很愚蠢.我提前道歉.
我有一个列表说@data,其中包含从输入中读取的行列表.这些行包含由(未知数量)空格分隔的数字.
现在,我想对它们进行排序并打印出来,但不是按字典顺序排列,而是根据行上出现的第一个数字的数值.
我知道这一定是非常简单但我无法弄明白该怎么做?
提前致谢,
我有一个非常简单的bash脚本来计算文件每一行出现的数字总和(我知道有更好的方法可以做到这一点,但我实际上需要这个总和作为辅助信息,脚本应该更晚些时候).脚本如下:
TOTAL=0;
cat $DATAFILE | while read LINE;
do
COUNT=`echo $LINE| awk '{print $2;}'`;
TOTAL=$((TOTAL+COUNT));
done
echo "Total = $TOTAL";
Run Code Online (Sandbox Code Playgroud)
但是,我总是得到输出"Total = 0".令人惊讶的是,如果我在while循环中移动最后一行,我会得到正确的结果.例如,如果输入文件包含
A 5
B 3
C 6
Run Code Online (Sandbox Code Playgroud)
我得到了输出
Total = 5
Total = 8
Total = 14
Run Code Online (Sandbox Code Playgroud)
但是当前版本总是输出0.似乎分配给变量TOTAL的值在某种程度上会丢失.
任何人都可以帮我解决这个问题吗?
提前致谢
首先,我对这个问题的标题含糊不清表示歉意。考虑以下示例:
class A {
public:
virtual void foo() const = 0;
void foo(int n) const {
while(n--)
foo();
}
};
class B: public A {
public:
void foo() const override {
std::cout << "B::foo()" << std::endl;
}
};
int main() {
auto obj = B{};
obj.foo();
obj.foo(10);
}
Run Code Online (Sandbox Code Playgroud)
基本上,我想foo在类A(以及任何子类)中使用该方法的两个版本。一个没有参数的参数必须由孩子定义(因此将其声明为pure)。但是,每个孩子都不需要重新定义一个带有整数的整数(因为功能相同:调用无参数版本n次数)。
但是上面的代码无法编译,给出以下错误:
g++ -D_GLIBCXX_DEBUG -std=c++11 example.cpp -o example
example.cpp: In function ‘int main()’:
example.cpp:31:15: error: no matching function for call to ‘B::foo(int)’
31 | obj.foo(10);
| …Run Code Online (Sandbox Code Playgroud) c++ ×7
sorting ×2
bash ×1
c++11 ×1
inheritance ×1
overloading ×1
perl ×1
polymorphism ×1
python-3.x ×1
reference ×1
std ×1
stdarray ×1
stdvector ×1
stl ×1
templates ×1
type-hinting ×1