you can compute it by dividing by 100 (removes the last 2 digits) and then mod'ing by 10 (only keeps the last digit)
(x / 100) % 10
Ancient Dragon commented: I missed that :) +28
bugmenot 25 Posting Whiz in Training
you can compute it by dividing by 100 (removes the last 2 digits) and then mod'ing by 10 (only keeps the last digit)
(x / 100) % 10
//vector initialized to allocate 10 string objects vector<string> *pvec1 = new vector<string>(10); delete pvec1;
I am not sure what you mean by "allocate 10 string objects". There are no string objects here at all. This is just a special functionality of vectors that reserves enough space to hold 10 string objects without resizing.
1> Would executing the preceding code create a memory leak? I am adding a string object to the first vector<string> object -- wouldn't that allocate more memory on the heap? If so, then wouldn't I have to execute:
delete pvec[0];
? Which doesn't work.No. The vector takes care of deallocating any memory it uses when it is destructed, which also destructs all the things in the vector. The string takes care of deallocating any memory it uses when it is destructed.
The "delete []" takes care of deallocating memory and destructing all the things in the array.
2> Is there a way to allocate (using new) 10 vector<string> objects, each with 10 string objects allocated? Such as:
vector<string> **pvec2 = new vector<string>(10)[10];
? Which also doesn't work.Thanks!
No. Unfortunately, when you create an array of something in C++, it needs to have a default constructor, and all elements will be default-constructed. So you cannot use a custom constructor to construct elements of an array. This is related to the RAII idiom in C++, where everything has to be initialized when it is allocated; and in this case there is just not any convenient way to customly initialize …
Reflection. you need it when you want to invoke a method by reflection. A method does not have to be public to be invoked. However, you will need to know how to get it. (Class.getMethod() will not work, it only works for public methods; Class.getDeclaredMethod() will work)
The standard way to do it with inheritance is probably to have an array of Employee pointers
Employee* employees[10];
(You can also dynamically allocate it if you really wish. It will be like
Employee** employees = new Employee*[10];
)
and then put whatever employees you want in there
employees[0] = new Manager("John","Doe",12,25,1950,2,14,1999, 3500, 1, "Accounting");
employees[1] = new Salesworker("John","Doe",12,25,1950,2,14,1999, 3, 4, 2, 9, 1);
// etc.
and then have a virtual method in the Employee class called "print" or something, which prints the name; each subclass can override it to do custom printing if they wish. Then just call it on every object pointed to in the array.
for (int i = 0; i < 10; i++)
employees[i]->print(); // assuming every employees[i] points to a valid object
Well.... ok, but just this once ;)
Private means that the variable is only accessible inside the class. Public means it can be accessed by anyone.
Here's an example to make things clearer:class DistSign { private: int priv; public: int publ; void ShowMe(void) { std::cout << "private: " << priv << " public: " << publ << "\n"; } }; int main() { DistSign D; std::cout << "private: " << D.priv << " public: " << D.publ << "\n"; cin.get(); return 0; }
When compiling this code you'll get an error on line 17, but not on line 10. That's because the line 10 is inside the class and 17 is not.
that's not what was asked
If there is truly a legitimate reason for a subclass to want to access a field, it should be declared "protected".
Is there any programming reason why I always see function definitions with arguments like this:
char getchar(char *arr){
Instead of this?:
char getchar(char arr[]){
These are identical. It is a pointer in both cases. So I guess it may be more clear to write it as a pointer. The second case might cause beginners to think that you are passing an entire array by value or something like that.
Also this code:
const char anonymous[ ] = "123"; char *arr = anonymous;
Didn't work for me, it gives a " invalid conversion from `const char*' to `char*'" compile error.
Yes. Either "arr" needs be changed to be declared as "const char *", or "anonymous" needs be changed to be declared as "char []".
Would it be correct to say that char *arr = "123"; is like this to the compiler:
char anonymous[ ] = "123"; char *arr = anonymous;
Without the constant declaration?
No. There are two special syntaxes here. But they are very different. The line 'char *arr = "123" ' makes a string literal (which is stored specially in some special part of memory by the compiler, and exists for the entire duration of the program) and gives the location of that to the pointer. The second code example creates an array which is a local variable of the current function, and initializes it. As a local variable, it only exists until the function returns, so it would be bad to return a pointer to it or refer it …
This seemed inefficient to me, so I looked around for another way. Here is a simpler way to do it, using the sqlite3.Cursor.description attribute.
from sqlite3 import dbapi2 as sqlite
cur.execute("SELECT * FROM SomeTable")
col_name_list = [tuple[0] for tuple in cur.description]
cur.description returns a tuple of information about each table. The entire tuple is : (name, type_code, display_size, internal_size, precision, scale, null_ok)
template<typename T, typename U> void print_first_five( map<T,U> m ) { for (int i = 0; i < (m.size() >= 5) ? 5 : m.size(); i++) cout << m[i].second; }
Hope this helps.
This is wrong, by the way. "m" looks up "i" as a key in the map. So it will not only fail to compile when "T" is not "int"; but even when it is an int, the binding might not exist.
I am under the impression that when you declare a variable of a complex data type (not sure if that's the right word, I mean like a self-defined class vs. an int) it is automatically a pointer.
Yes, they are called "reference types". The references point to objects.
.
You cannot make it so that it is not a pointer.
You mean that you cannot have objects as values, then yes.
And when you declare a variable with a simple data type, it is not a pointer
Yes, they are called primitive types.
and nothing you can do will make it a pointer?
Basically. There is no way to "take the address" of a variable. But you can copy the value of the variable into a field of a wrapper object, which then can be pointed to by a reference.
Like, this java code (sorry if there are minor syntax errors, as I'm used to C++):
Student bill; bill = new Student(); bill.GPA = 4.0;
is equivalent to this C++ code:
Student* bill; bill = new Student; bill->GPA = 4.0;
Is that a correct "translation"?
Yes.
it's right. vector doesn't have a method named "length()". it does have a method named "size()".
what i was thinking was could i just do
vertices = new float[v][3];
this one only works if the "3" part is a constant, and always creates a "rectangular" array made of contiguous parts, no pointers involved
Doesn't work :( .
Next time, at least put in minimal effort and post an error or something; instead of other people repeatedly posting suggestions, and you just saying "doesn't work" each time without any clue as to what doesn't work.
It does not need an uninstall code on Vista, but it does on XP. I suspect that it will soon need a code on Vista though.
If you call them at the 800 help number on their site, they can help you regain the admin password.
how about
root = new Node<K>(newKey, NULL, NULL);
Also, a fundamental problem with your grow() function:
void grow(int array[], int newnumber, int oldsize)
{
//...
array = temp; // swap contents back to array
}
The last line is completely useless, because "array" is a local variable, assigning it has no effect outside the function.
So these are all very self-explanatory. What part of the errors don't you understand?
Tree.cpp:95: error: no matching function for call to `TreeType<int>::Find(int&)'
bintree.h:235: note: candidates are: void TreeType<ItemType>::Find(ItemType&, bool&) [with ItemType = int]
Apparently Find() takes two arguments (a bool reference as a second argument) and doesn't return anything; instead of taking one argument and returning an int.
Tree.cpp:104: error: no matching function for call to `TreeType<int>::PrintInOrder()'
bintree.h:267: note: candidates are: void TreeType<ItemType>::PrintInOrder(std::ostream&) const [with ItemType = int]
You didn't show this code. But it looks like you are calling PrintInOrder(), which takes one argument (an ostream), without any arguments.
Tree.cpp:116: error: expected primary-expression before '&' token
Tree.cpp:116: error: `outFile' was not declared in this scope
Tree.cpp:116: warning: unused variable 'outFile'
You wrote some type and variable name in the middle of the expression, as if you were declaring some variable or something. I have no idea what you were trying to do here.
the second and fourth const are utterly and completely pointless. there is never a point to declare that a value passed by value is not going to be modified. you should remove them
static local variables and static data members have lifetimes like global variables (there is only one copy in the program, and it exists for the whole duration of the program), except that their scope is not global
sum should be set to 0 at the beginning of every iteration of the outer loop
and i is only incremented at every block of three, so "bin" is the same all three times
sphere::sphere() //default constructor { int Radius=0; }
wow.
what did you expect that code to do?
1) when using vectors you can access individual elelements just as you would do with a simple int array
B = LiDARGrid.ConcentricScans.[scancounter].getColumn(concentriccol);
except that it doesn't check bounds
opOverload obj1 = (10,20); opOverload obj2 = (30,40);
should probably be
opOverload obj1(10,20);
opOverload obj2(30,40);
I need to compile my program everytime I want them to run
No. You compile it once to .class files. Then you run the .class files with "java MyClass".
if you need to delete arbitrary elements a lot, then perhaps you don't want to use a vector, because it is very expensive, both to find it and to shift all the remaining elements
and why can't you just tell us what is wrong with it?
a completely literal translation into C++ would be like this:
class Node{
private:
void *item;
Node *next;
public:
Node(void *obj) : item(obj) { }
void *getItem() const { return item; }
Node *getNext() const { return next; }
void setItem(void *obj) { item = obj; }
void setNext(Node *n) { next = n; }
};
you could either get all the digits out into a list or something, and then parse them back into a number in reverse
or just take the input as a string and reverse the string
why don't you post code that actually compiles?
float* matrixa = new float(6);
you probably wanted
float* matrixa = new float[6];
note that with any two numbers a and b, a * b = gcd(a, b) * lcm(a, b)
start with this
bool discr(double array2[3], double &result)
do you know how to use c++ references?
why don't you post the exception and stack trace, and post the code inside code tags
no, it is better that you make the class a functor class (its has an overloaded function call operator); and then you should template the sort function, so it can either take a function pointer or a function object
int compare(int a, int b)
{
return a - b;
}
template <typename Compare>
void sort(int *ar, Compare pf)
{
//a sorting algorithm.
}
class MyClass
{
bool condition;
public:
int operator()(int a, int b)
{
if (condition)
return a - b;
else
return b - a;
}
};
void main()
{
int testAr[] = {0, 1, 2, 3};
sort(testAr, compare);
sort(testAr, MyClass);
return 0;
}
int returnVal = null;
also this code is wrong
The first piece of code works because "mtab" is an statically-sized array which the compiler knows the size at compile time. (The macro is just a convenient compile-time trick.) The second piece of code doesn't work because "myclass->smath" is a pointer, and there is no way to tell how many things are in an array at runtime if you only know where the first element is. Arrays and pointers are not the same.
It says 'This will allow to practice with ArrayList and allow you to use generics, if you wish.'
Well are you using Generics? Maybe you should.
http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html
void function(int& aaa[][], int 1d, int 2d)
{
for(int i=0; i<1d; i++)
{
for(int j=0; j<2d; j++)
{
aaa[j] = i*j;
}
}
}
This is wrong, for many reasons. First, this is an array of arrays of references. The caller is not going to be able to pass a multidimensional array of ints in to that. Second, you cannot have "[][]"
in the parameter list; all but the last dimension must have a constant dimension in it (the last []
gets converted to a pointer).
in java objects are always referred to using "references", which are kind of like pointers in C, except that you can't do silly stuff with them
what problem are you trying to solve? and why do you need to remove elements?
can we see more code? for example, what types are all these things? how are they defined?
You can't really delete an arbitrary item from a priority queue easily; only the item with the highest priority. Maybe you should read up on what priority queues are:
http://en.wikipedia.org/wiki/Priority_queue
The items in the priority queue (implemented as a binary heap) have some structure, but are not in sorted order; so your method will not work.
Perhaps you should be using sets instead. They can remove items easily.
if(hash.containsKey(hash.get(info)))
This line makes no sense; you are getting the value that is associated with the key "info", and then you are testing whether that value is a key in the hash table?
Nor can you use arrays.
no
maybe you are using a C compiler instead of C++
um... where is your code?
all wrong :P
screw you
x.multiply(y)
why don't you show us what your problems are