Questions tagged [construction]
Use for questions related to building a data structure, such as a heap.
construction
115
questions
877
votes
19
answers
497k
views
How can building a heap be O(n) time complexity?
Can someone help explain how can building a heap be O(n) complexity?
Inserting an item into a heap is O(log n), and the insert is repeated n/2 times (the remainder are leaves, and can't violate the ...
16
votes
3
answers
3k
views
What is an int() Called?
It's been rehashed over and over that primitive types don't have constructors. For example this _bar is not initialized to 0 when I call Foo():
class Foo{
int _bar;
};
So obviously int() is not ...
12
votes
9
answers
17k
views
Programmatic HTMLDocument generation using Java
Does anyone know how to generate an HTMLDocument object programmatically in Java without resorting to generating a String externally and then using HTMLEditorKit#read to parse it? Two reasons I ask:
...
11
votes
2
answers
194
views
Referencing the same variable that you're declaring
I've seen the following type mistake a couple of times while working with C++ code:
QString str = str.toUpper();
This can be a fairly easy mistake to make and yet it compiles and executes (sometimes ...
10
votes
1
answer
6k
views
Is there a difference in how member variables are initialized in Dart?
In Dart, is there a difference in assigning values right away vs in constructor like in Java?
class Example {
int x = 3;
}
vs
class Example {
int x;
Example() {
x = 3;
}
}
I ...
10
votes
2
answers
331
views
Shouldn't the temporary A(3) be destroyed before "Here" is printed?
Shouldn't the temporary A(3) be destroyed before "Here" gets printed?
#include <iostream>
struct A
{
int a;
A() { std::cout << "A()" << std::endl; }
A(int a) : a(a) { ...
9
votes
3
answers
12k
views
jquery widget, _create or _init
Some jquery plugin extend widget use _create method, while others use _init method, can someone explain the differences between the two?
Also any guidance on when it is better to extend widget or ...
9
votes
5
answers
2k
views
Is Object constructor called when creating an array in Java?
In Java, an array IS AN Object. My question is... is an Object constructor called when new arrays is being created? We would like to use this fact to instrument Object constructor with some extra ...
8
votes
1
answer
1k
views
How to in-place-construct an optional aggregate?
How to in-place construct an optional aggregate? It seems I can only construct an optional single thing, and not an optional aggregate of things.
#include <optional>
#include <iostream>
...
8
votes
5
answers
5k
views
Abstract syntax tree construction and traversal
I am unclear on the structure of abstract syntax trees. To go "down (forward)" in the source of the program that the AST represents, do you go right on the very top node, or do you go down? For ...
8
votes
2
answers
732
views
Construction of temporary in function call is interpreted as declaration
Lately I ran into a problem which somehow (but only somehow) makes sense to me. It is based on interpreting the construction of a temporary as declaration of the single (!) constructor argument. ...
7
votes
3
answers
2k
views
Building a Binary Tree (not BST) in Haskell Breadth-First
I recently started using Haskell and it will probably be for a short while. Just being asked to use it to better understand functional programming for a class I am taking at Uni.
Now I have a slight ...
7
votes
1
answer
9k
views
thread_local member variable construction
I'm facing some strange behavior with thread_local and not sure whether I'm doing something wrong or it's a GCC bug.
I have the following minimal repro scenario:
#include <iostream>
using ...
7
votes
4
answers
3k
views
Understanding the efficiency of an std::string
I'm trying to learn a little bit more about c++ strings.
consider
const char* cstring = "hello";
std::string string(cstring);
and
std::string string("hello");
Am I correct in assuming that both ...
6
votes
4
answers
1k
views
Other way to prohibit a certain C++ class construction except than declaring the constructor private?
Say I have a class with some const reference member variable and I would like to forbid a certain type of construction. So I would declare the according constructor private. Of course, a constructor ...
6
votes
1
answer
200
views
Can a C++ constructor know whether it's constructing a const object?
In C++, object constructors cannot be const-qualified.
But - can the constructor of an object of class A know whether it's constructing a const A or a non-const A?
Motivated by a fine point in the ...
6
votes
2
answers
448
views
How to support multiple construction signatures in a factory design?
I'm working with the following (simplified) factory design to create objects of some inheritance hierarchy, shouldn't be anything special:
// class to create
class Class
{
public:
Class(Type type, ...
5
votes
2
answers
11k
views
Weird "candidate expects 1 argument, 0 provided" in constructor
I'm making a simple threaded server application in C++, thing is, I use libconfig++ to parse my configuration files. Well, libconfig doesn't support multithreading, thus I'm using two wrapper classes ...
5
votes
3
answers
3k
views
Idiom to initialize C++ class to zero
Consider the following C++ class:
struct Point
{
int x;
int y;
explicit Point() =default; // 1
explicit Point(int x_, int y_): x(x_), y(y_) { } // 2
};
The second constructor is ...
5
votes
3
answers
1k
views
C++ temporary variable lifetime
Is this code valid?
int foo()
{
std::vector<std::string>& v = std::vector<std::string>(5, "X");
// Do something silly...
return 42;
}
For some reason I thought that the ...
5
votes
4
answers
3k
views
Giant switch statement for constructors
I have a container which holds a bunch of pointers to a base class, and a function which takes some input and returns a class which is a subclass of the base class. Which subclass it returns depends ...
5
votes
2
answers
219
views
Are the following 3 ways to define objects identical?
In my understanding the following are identical:
Person p{}; // Case 1
Person p = {}; // Case 1.5
I noticed
Person p = Person{}; // Case 2
produces the same tracing output as the Case 1 and Case 1.5 ...
4
votes
10
answers
9k
views
Emptying a C++ object
Often I add an Empty method to my C++ objects to clear the internal state using code similar to the following.
class Foo
{
private:
int n_;
std::string str_;
public:
Foo() : n_(1234), ...
4
votes
2
answers
301
views
Utilizing move-semantics and element-wise initialization of containers
Often seen that examples of using STL algorithms are illustrated with list-initialized containers like:
std::vector< int > v{1, 2, 3, 4};
But when this approach is used for (heavyweight) ...
4
votes
1
answer
1k
views
I am getting 'Local Search phase started with an uninitialized Solution' when I run on a larger dataset
I am developing a solver using Optaplanner 6.1.0, similar to the Vehicle Routing Problem. When I run my solver on 700 installers and 200 bookings, it will successfully solve the planning problem. But, ...
4
votes
1
answer
3k
views
C++11 enum class instantiation
I've encountered the following form of enum class variable instantiation and it is compiling without any warning or error under VS2012:
UINT32 id;
enum class X {apple, pear, orange};
X myX = X(id);
...
3
votes
4
answers
724
views
Deserialize a binary tree breadth-first in Functional Programming
I had an imperative program which deserializes to a Binary Tree from an array. Its a BFS algorithm. I was wondering how to do this in Scala with functional programming concepts.
class TreeNode(...
3
votes
5
answers
215
views
When is an object in Javascript constructed?
Consider the following Javascript function (1):
function setData(domElement) {
domElement.myDataProperty = {
'suppose': 'this',
'object': 'is',
'static': 'and',
'pretty': 'big'
};
...
3
votes
1
answer
3k
views
At which phase is managed bean constructed and which constructor is used
Consider example of JSF based web-app hello1 from official tutorial with addition constructor in managed bean. The follow index.xhtml facelet
<html lang="en"
xmlns="http://www.w3.org/1999/...
3
votes
2
answers
2k
views
Multiple factory methods versus single method
Is it better to use a single factory method and a general constructor for all instances, then populate the instances? OR should multiple factory methods and constructors be used instead? What are the ...
3
votes
2
answers
3k
views
How to construct an octree in C++
I am implementing an Octree in C++ which should later contain a mesh for rendering. But at the moment I am struggeling with the construction of the Octree. To be more precisely it is the addNode() ...
3
votes
1
answer
391
views
STL: Initializing a container with an unconstructed stateful comparator
This has been running through my mind as a possible solution to an issue, however as it is a fairly obvious technical violation of something in C++, I wanted to know how likely to it is to fail, ...
3
votes
2
answers
519
views
Construction vs Initialisation Formal difference
I am learning C++ using the books listed here. Now I came across the following statement from C++ Primer:
When we allocate a block of memory, we often plan to construct objects in that
memory as ...
2
votes
3
answers
1k
views
Can a java subclass's private final field be initialized before the super constructor completes?
I have a pair of classes looking like this;
public abstract class Class1 {
//...
public Class1() {
//...
function2();
//...
}
protected abstract void function2()...
2
votes
3
answers
597
views
Hard-coding Fonts in DotNET
I've run into problems on numerous occasions of users not having one of the Windows Core fonts installed on their machine (Courier New, Comic Sans MS, Arial for example). Whenever I try to construct ...
2
votes
1
answer
2k
views
Construct incorrect URLs in java with java.net.URL?
Using oracle java 1.8.0_25
I have the following construct
URL url = new URL(new URL(new URL("http://localhost:4567/"), "123"), "asd")
According the documentation in ...
2
votes
1
answer
72
views
Range Construction Pattern
Given the following code
import std.datetime: Clock, SysTime, Duration;
SysTime[] times;
const n = 3;
foreach (i; 0..n) times ~= Clock.currTime;
is there a simpler, perhaps functional, higher order ...
2
votes
1
answer
10k
views
Create mesh from point cloud
I have a set of points that need to be constructed as a set of triangular faces to form a solid mesh. I have looked at Delaunay Triangulation and none of it makes sense.
Any advice on where to begin? ...
2
votes
2
answers
388
views
In place constrution of member variable via constructor
Take the following class:
template <typename TPayload>
class Message
{
public:
Message(const TPayload& payload)
: m_header(sizeof(TPayload)),
...
2
votes
2
answers
7k
views
Prolog; if and (stopping) recursion
In trying to better understand prolog, lists and recursion as a whole I'm working my way through various simple tasks I've assigned to myself.
Among others is removing double entries from a list.
I'...
2
votes
3
answers
3k
views
How to specify which columns can be returned from linq to sql query
I'm trying to only return a few columns from a linq to sql query but if I do, it throws the exception:
Explicit construction of entity type 'InVision.Data.Employee' in query is not allowed
Here's ...
2
votes
1
answer
81
views
Haskell fast construction of vector from input of space separated ints
If I expect a string with space separated ints from user input (number of expected int's known), I can transfer it in haskell Data.Vector like this:
main = do
n <- readLn -- this is number ...
2
votes
1
answer
616
views
What could cause the destructor of a parent to be called during construction of the child?
enter code hereI am seeing segfaults in a strange part of my code, and after using valgrind, it seemed the problem was the destructor of a parent being called during construction of the child. This is ...
2
votes
2
answers
3k
views
./configure--with-boost no such file or directory
When I used ./configure the terminal returned:
checking for Boost headers version >= 1.41.0... no configure: error:
cannot find Boost headers version >= 1.41.0
So i used the command "./...
1
vote
3
answers
3k
views
Best way to represent Bit Arrays in C#?
I am currently building a DHCPMessage class in c#.
RFC is available here : http://www.faqs.org/rfcs/rfc2131.html
Pseudo
public object DHCPMessage
{
bool[8] op;
bool[8] htype;
bool[8] ...
1
vote
2
answers
430
views
Construct ImmutableSortedSet without warnings
I want construct ImmutableSortedSet. I wrote code smt like:
Set<String> obj = new HashSet<String>();
Comparator<String> myComparator = new Comparator<String>(){
@Override
...
1
vote
2
answers
362
views
C++ referring to an object being constructed
In C++ I have a reference to an object that wants to point back to its owner, but I can't set the pointer during the containing class' construction because its not done constructing. So I'm trying to ...
1
vote
2
answers
4k
views
Recursive function building a vector, what will be returned?
so I'm trying to build a vector recursively, as I look at this I begin to think I'm doing it quite wrong. Will the following code return a vector with each iterations results, or am I just creating ...
1
vote
2
answers
499
views
Delay true base class construction with placement new
I'm asking if (and why) the following approach is a) legal and b) moral. I'm asking with emphasis on C++03, but notes on C++11 are welcome, too. The idea is to prevent derived classes that could ...
1
vote
3
answers
233
views
How do I initialize all elements of TrieNodes' children to null
I am trying to solve a Trie problem, for which I create a TrieNode class as below:
class TrieNode {
public:
bool isWord;
TrieNode* children[26];
TrieNode() {
isWord=false;
...