我正在尝试使用外部库安装到我的C++项目中Cmake.我希望Xcode用该库生成项目.在我的终端中,我从构建目录运行以下命令:
cmake -G Xcode ..
Run Code Online (Sandbox Code Playgroud)
这给了我以下错误:
-- The CXX compiler identification is unknown
-- The C compiler identification is unknown
CMake Error at CMakeLists.txt:6 (project):
No CMAKE_CXX_COMPILER could be found.
CMake Error at CMakeLists.txt:6 (project):
No CMAKE_C_COMPILER could be found.
Run Code Online (Sandbox Code Playgroud)
我正在使用g ++编译器:
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 7.0.0 (clang-700.1.76)
Target: x86_64-apple-darwin14.5.0
Thread model: posix
Run Code Online (Sandbox Code Playgroud)
编辑:CMakeLists.txt文件
file(READ "${CMAKE_CURRENT_SOURCE_DIR}/VERSION" PROJECT_VERSION_FULL)
string(REGEX REPLACE "[\n\r]" "" PROJECT_VERSION_FULL "${PROJECT_VERSION_FULL}")
string(REGEX REPLACE "^([0-9]+)\\.[0-9]+\\.[0-9]+$" "\\1" PROJECT_VERSION_MAJOR "${PROJECT_VERSION_FULL}")
string(REGEX REPLACE "^[0-9]+\\.([0-9]+)\\.[0-9]+$" "\\1" …Run Code Online (Sandbox Code Playgroud) 我只是在Codility,并遇到了一个任务,我无法找到有针对性的O(n)效率的解决方案; 我的解决方案运行O(n2).如果有人能给我一个关于如何让它跑得更快的提示,我会很高兴的.这是任务.
给出了由N个整数组成的非空零索引数组A.
单调对是一对整数(P,Q),使得0≤P≤Q<N且A [P]≤A[Q].
目标是找到其指数最远的monotonic_pair.更确切地说,我们应该最大化Q-P的值.仅找到距离就足够了.
例如,考虑数组A,使得:
A[0] = 5
A[1] = 3
A[2] = 6
A[3] = 3
A[4] = 4
A[5] = 2
Run Code Online (Sandbox Code Playgroud)
有11个monotonic_pairs:(0,0),(0,2),(1,1),(1,2),(1,3),(1,4),(2,2),(3, 3),(3,4),(4,4),(5,5).对中的最大距离是3(1,4).
写一个函数:
class Solution {public int solution(int [] A); }
如果给定N个整数的非空零索引数组A,则返回任何monotonic_pairs中的最大距离.
例如,给定:
A[0] = 5
A[1] = 3
A[2] = 6
A[3] = 3
A[4] = 4
A[5] = 2
Run Code Online (Sandbox Code Playgroud)
该函数应返回3,如上所述.
假使,假设:
N是[1..300,000]范围内的整数; 数组A的每个元素都是[-1,000,000,000..1,000,000,000]范围内的整数.复杂:
预期的最坏情况时间复杂度是O(N); 预期的最坏情况空间复杂度是O(N),超出输入存储(不计入输入参数所需的存储).可以修改输入数组的元素.
我的第一个想法解决方案(在O(n2)中运行):
public static int solution(int[] A) {
int max = 0;
for(int i=0; i<A.length-1; i++) {
for(int j=i+1; …Run Code Online (Sandbox Code Playgroud) 我有一个自定义 Stack 实现的示例。据我了解,.h文件应该只包含声明,cpp文件应该只包含implementations。我在cplusplus.com/stack_example上找到了一个自定义堆栈的例子,它看起来像下面这样。
堆栈.h文件
#ifndef _STACK_H_
#define _STACK_H_
#include <iostream>
#include "Exception.h"
template <class T>
class Stack {
public:
Stack():top(0) {
std::cout << "In Stack constructor" << std::endl;
}
~Stack() {
std::cout << "In Stack destructor" << std::endl;
while ( !isEmpty() ) {
pop();
}
isEmpty();
}
void push (const T& object);
T pop();
const T& topElement();
bool isEmpty();
private:
struct StackNode { // …Run Code Online (Sandbox Code Playgroud) 我试图在我的模拟对象上使用 Mockito,它应该始终返回作为参数传入的相同对象。我试过这样做:
private val dal = mockk<UserDal> {
Mockito.`when`(insert(any())).thenAnswer { doAnswer { i -> i.arguments[0] } }
}
Run Code Online (Sandbox Code Playgroud)
但是,这条线总是失败:
io.mockk.MockKException: no answer found for: UserDal(#1).insert(null)
Run Code Online (Sandbox Code Playgroud)
该insert(user: User)方法不null作为参数接受(显然User不是可空类型)。
如何使该insert()方法始终返回它作为参数接收的相同对象?
我正在从文件中读取我的结构,我想将它们添加到结构的向量中.以下是它的外观和工作原理:
typedef struct
{
int ID;
string name;
string surname;
int points;
}
Student;
int main()
{
ifstream theFile("test.txt");
std::vector<Student*> students;
Student* s = new Student();
while(theFile >> s->ID >> s->name >> s->surname >> s->points)
{
studenti.push_back(s); // here I would like to add this struct s from a file
}
// here I want to print each struct's values on the screen, but the output is always ONLY last struct N times, and not all of them, each only …Run Code Online (Sandbox Code Playgroud) 我在git源代码管理下建立了一个团队项目,团队的所有成员都将使用不同的IDE.该项目是一个基本的命令行应用程序C.我个人想在Xcode7中工作,但是我的xcodeproj文件(或文件夹)也有问题.我不想要这个,因为其他团队成员不使用Xcode,这个xcodeproj文件只是在他们结账时弄得一团糟.我只是希望能够提交我的常规源文件,git而不是其他任何东西.我该怎么办?如果答案是.gitignore文件,它应该怎么样?
我将我的模拟定义如下:
private val dal = mockk<UserDal> {
every { insert(any()) } returnsArgument 0
}
Run Code Online (Sandbox Code Playgroud)
然后,我尝试像这样测试它:
@Test
fun test() {
userService.registerUser(userJohn)
verify(dal).insert(check {
assertEquals(it.firstName, "John")
})
}
Run Code Online (Sandbox Code Playgroud)
这会引发异常:
org.mockito.exceptions.misusing.NotAMockException:
Argument passed to verify() is of type UserDal and is not a mock!
Make sure you place the parenthesis correctly!
Run Code Online (Sandbox Code Playgroud)
我不明白它怎么会说这UserDal不是模拟,而它显然是模拟!这段代码有什么问题?如何验证参数字段?
我会努力做到这一点.
我有我的自定义Node对象,它具有属性Cost.我想按属性Cost按升序对这些Node对象进行排序.
我能够这样做PriorityQueue<Node> = new PriorityQueue<Node>(10000, new NodeComparator());,但这种方式对我来说太慢了,现在我希望做同样的事情,只使用TreeSet.无论如何,如果我的构造函数看起来像这样TreeSet<Node> = new TreeSet<Node>(new NodeComparator());,程序似乎跳过了大量的Node对象,似乎将它们视为相同.他们不是.我假设可能存在一些hashCode问题,但我不确定,我现在不知道如何解决它.
简而言之,我只是希望TreeSet中的节点按Cost属性按升序排序.这是我的NodeComparator类:
public class NodeComparator implements Comparator<Node> {
@Override
public int compare(Node n1, Node n2) {
// TODO Auto-generated method stub
if(n1.cost > n2.cost) return 1;
else if(n1.cost < n2.cost) return -1;
else return 0;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的Node类:
public class Node{
public State state;
public int cost;
public Node(State s, int Cost){
this.state = s;
this.cost = Cost;
}
public State getState(){
return this.state;
} …Run Code Online (Sandbox Code Playgroud) 假设我有一个结构节点,如下所示:
typedef struct node{
int t;
double p;
}node;
Run Code Online (Sandbox Code Playgroud)
然后我也有struct node一些数组具有相等的值p.
我想做的是:
首先,根据值p对元素进行排序.在我有一个基于p的排序数组之后,我希望每个具有相同p的子数组都基于值t进行排序.
例如:
如果我有这样的原始数组(第一个元素是p,第二个元素是t):
[0,10 | 1],[0.05 | 0],[0,10 | 0],[0,05 | 2],[0,10 | 2],[0,15 | 1],[0,05 | 1]
在进行双重排序后,它应如下所示:
[0,05 | 0],[0,05 | 1],[0,05 | 2],[0,10 | 0],[0,10 | 1],[0,10 | 2],[0 ,15 | 1].
我已经提出了基于p的冒泡排序,但是我在如何对t上的子数组进行排序方面遇到了困难.这是p上的冒泡排序代码.
node *sort_p(node *nodes, int num) {
int i, …Run Code Online (Sandbox Code Playgroud) 因此,ClassCastException当我试图将我投入我的时Node<T>,我会得到这个AVLNode<T>.不知道为什么.
这是它抛出异常的地方.
Node<Integer> n1 = new Node<Integer>(3, null);
AVLNode<Integer> n2 = (AVLNode<Integer>) n1;
Run Code Online (Sandbox Code Playgroud)
课程看起来如何......
public class Node<T extends Comparable<T>> {
public Node<T> right;
public Node<T> left;
public Node<T> parent;
public T data;
public Node(T data, Node<T> parent) {
this.data = data;
this.parent = parent;
} // ...
Run Code Online (Sandbox Code Playgroud)
而另一个类在单独的文件中:
public class AVLNode<T extends Comparable<T>> extends Node<T> {
public int height = 1;
public AVLNode(T data, Node<T> parent) {
super(data, parent);
} //...
Run Code Online (Sandbox Code Playgroud)
错误消息:线程"main"中的异常java.lang.ClassCastException:custom.trees.Node无法强制转换为custom.trees.AVLNode
我遇到了一个相当混乱的问题.看起来我程序中的IF语句导致了分段错误.
我正在使用外部库,并在IF语句中调用外部库中的代码,因此我无法提供这些函数的完整代码,因为我也没有.
会发生什么的基本例子.所以这个例子引起了我的兴趣Segmentation fault.
IRank *rank;
//Generating wavelet tree from BWT with sdsl library
if(true) {
std::cout << "I am in IF" << endl; // this gets printed on the screen
wt_huff<> wt; // right after that - segm fault
construct_im(wt, BWT, 1);
WTRank wtrank(&wt);
rank = &wtrank;
}
Run Code Online (Sandbox Code Playgroud)
但是,同样的例子,但没有IF,当我评论它时,不会导致Segmentation fault并正常执行.
IRank *rank;
//Generating wavelet tree from BWT with sdsl library
//if(true) {
std::cout << "I am in IF" << endl; // again this gets printed …Run Code Online (Sandbox Code Playgroud) 我有一个问题,一个方法应该在二叉树中找到一个包含给定的节点value.下面提供的方法不起作用,问题是为什么.
public Node search(Node node, int value) {
if(node.value == value) return node;
if(node.left != null) search(node.left, value);
if(node.right != null) search(node.right, value);
return null;
}
Run Code Online (Sandbox Code Playgroud)
问题是,null当value树中实际存在给定节点时,此方法有时会返回.这是为什么?