int main()
{
void* Foo = new???
delete Foo;
}
Run Code Online (Sandbox Code Playgroud)
你怎么做上面这样的事情?你不能放new void[size]
.我不想知道怎么做malloc()
和free()
.我已经知道这很有效.我很好奇,想知道如何用new和delete完成它.
我用Google搜索并看到了一些东西operator new(size)
; 和operator delete(size)
;
这些和new
/ 之间有什么区别delete
?为什么C++不仅仅允许新的void* [size]
?
我正在尝试优化std::vector
"搜索" - 基于索引迭代向量并返回与"搜索"条件匹配的元素
struct myObj {
int id;
char* value;
};
std::vector<myObj> myObjList;
Run Code Online (Sandbox Code Playgroud)
使用唯一id
的值和值创建几千个条目并将它们推送到向量myObjList
.
什么是最有效的检索方式myObj
匹配id
.目前我的索引迭代如下:
for(int i = 0; i < myObjList.size(); i++){
if(myObjList.at(i).id == searchCriteria){
return myObjList.at(i);
}
}
Run Code Online (Sandbox Code Playgroud)
注意:searchCriteria = int
.所有元素都有独特id
之处.上面做的工作,但可能不是最有效的方式.
前几天我用Java编写了一个类来计算a point(X,Y)
是否在多边形内部.(X
和Y
是double
的,因为将地理坐标).
我知道,Java有类Polygon
,但我不得不使用Path2D
和Point2D
,因为Polygon
不允许double
的,只是整数:(
一旦我完成了多边形Path2D
,我就使用了方法contains
(Path2D
有了它),我的问题就解决了.
但现在,我想导入到Android,问题出在这里,因为Path2D
需要导入:
import java.awt.geom.Path2D;
import java.awt.geom.Point2D;
Run Code Online (Sandbox Code Playgroud)
并且在Android中不存在awt,所以我无法使用.
那么,有没有类似于Path2D
那种contains
方法的类?或者我必须自己计算?
以下是我在Java中使用的方法Path2D
:
private void ConstructPolygon(Vector<Point2D> coodinates)
{
this.polygon.moveTo(coodinates.get(0).getX(), coodinates.get(0).getY());
//System.out.println(coodinates.get(0).getX() + " " + coodinates.get(0).getY());
//System.out.println("asda");
for(int i = 1; i < this.num_points; i++)
{
//System.out.println(coodinates.get(i).getX() + " " + coodinates.get(i).getY());
this.polygon.lineTo(coodinates.get(i).getX(), coodinates.get(i).getY());
}
this.polygon.closePath();
}
public boolean InsideCity(Point2D …
Run Code Online (Sandbox Code Playgroud) 如何转换std::chrono::time_point
为字符串?例如:"201601161125"
.
是否可以创建一个带有可变数量参数的模板函数,例如,在此类Vector< T, C >
构造函数中:
template < typename T, uint C >
Vector< T, C >::Vector( T, ... )
{
va_list arg_list;
va_start( arg_list, C );
for( uint i = 0; i < C; i++ ) {
m_data[ i ] = va_arg( arg_list, T );
}
va_end( arg_list );
}
Run Code Online (Sandbox Code Playgroud)
这几乎可以工作,但如果有人调用Vector< double, 3 >( 1, 1, 1 )
,只有第一个参数具有正确的值.我怀疑第一个参数是正确的,因为它double
在函数调用期间被转换为a ,而其他参数被解释为int
s然后将这些位填充到a中double
.通话可以Vector< double, 3 >( 1.0, 1.0, 1.0 )
产生预期的效果.有没有一种首选方式来做这样的事情?
这可能是一个不成熟的问题,可能是我遗漏了一些东西,但我的问题是
试图转换char
到int
拿到ASCII的那个值char
,在大多数情况下,我得到正确/预期的ASCII的特定代码char
,在某些情况下,我不知道.有人能解释一下为什么吗?
例子:
// Example 1:-
Console.WriteLine((int)'a');
// gives me 97 perfect!
// Example 2:-
Console.WriteLine((char)1); gives me ?
// now
Console.WriteLine((int )'?');
// this should give me 1, instead it gives me 9786 why?
Run Code Online (Sandbox Code Playgroud)
这发生在ASCII> 127
或ASCII < 32
.
我有一个const-correctness问题,我似乎无法解决.这是我的程序的结构:
class Node
{
private:
int id;
std::set<Node*> neighbours;
public:
Node();
Node(int id_p);
void set_id(const int& id_p);
int get_id() const;
void add_neighbour(Node* neighbour);
bool is_neighbour(Node* neighbour) const;
friend bool operator <(const Node& lhs, const Node& rhs);
};
class Graph
{
private:
std::set<Node> node_list;
public:
Graph();
void add_node(int id);
const Node* get_node_by_id(int id) const;
bool has_node(int id) const;
void check_add_node(int id);
void add_edge(int id_1, int id_2);
bool has_edge(int id_1, int id_2) const;
void check_add_edge(int id_1, int id_2);
(...)
};
Run Code Online (Sandbox Code Playgroud)
现在问题是,如果我调用该函数Graph::get_node_by_id() …
我正在寻找一种方法将hex
(十六进制)转换为dec
(十进制).我找到了一个简单的方法,如:
int k = 0x265;
cout << k << endl;
Run Code Online (Sandbox Code Playgroud)
但随着我不能输入265
.无论如何它是这样的:
输入: 265
输出: 613
反正有吗?
注意:我试过了:
int k = 0x, b;
cin >> b;
cout << k + b << endl;
Run Code Online (Sandbox Code Playgroud)
它不起作用.
首先,我想说我是新手.
我想boost:multi_array
在课堂上初始化.我知道如何创建boost:multi_array
:
boost::multi_array<int,1> foo ( boost::extents[1000] );
Run Code Online (Sandbox Code Playgroud)
但作为课程的一部分,我遇到了问题:
class Influx {
public:
Influx ( uint32_t num_elements );
boost::multi_array<int,1> foo;
private:
};
Influx::Influx ( uint32_t num_elements ) {
foo = boost::multi_array<int,1> ( boost::extents[ num_elements ] );
}
Run Code Online (Sandbox Code Playgroud)
我的程序通过编译,但在运行期间,当我尝试从foo
(例如foo[0]
)控制元素时,我收到错误.
如何解决这个问题呢?
这段代码会爆炸,对吗?一旦循环退出,原始实例将与其所有内部成员一起死亡,因此如果它们不是POD,那么任何do_stuff
需要访问成员的方法B
都会抛出分段错误,对吗?
void foo() {
std::vector<B> bar;
for (int i = 0; i < 7; i++)
bar.push_back(B(i, i, i));
bar[3].do_stuff();
}
Run Code Online (Sandbox Code Playgroud)
那么,有没有办法在不使用指针的情况下做到这一点?或者你必须这样做:
void foo() {
std::vector<B*> bar;
for (int i = 0; i < 7; i++)
bar.push_back(new B(i, i, i));
bar[3]->do_stuff();
for (int i = 0; i < 7; i++)
delete bar[i];
}
Run Code Online (Sandbox Code Playgroud)