Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
tractor fanatic
Sep 9, 2005

Pillbug
Regarding std::basic_string, can I use this to build a string character by character using operator += or do I risk this being in O(n^2) time?

Adbot
ADBOT LOVES YOU

That Turkey Story
Mar 30, 2003

tractor fanatic posted:

Regarding std::basic_string, can I use this to build a string character by character using operator += or do I risk this being in O(n^2) time?

Why would it be O(n^2)? I can't even imagine what an algorithm for string concatenation would look like that would perform that poorly. Appending a single character is amortized constant time (realistically the buffer may be automatically resized on occasion, pending implementation, which would imply a memcpy).

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
I think he means O(n^2) for the entire process of building the string character-by-character, which is what you'd get if each append did a copy.

Anyway, the answer is that while the standard doesn't seem to guarantee that appending a char to a std::basic_string will have amortized constant complexity, I think all implementations do.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

Harokey posted:

Is there a way to create an exception class, that when thrown (and not caught) prints a meaningful error message?
std::set_terminate is probably supported on all of the platforms that you have to care about. In the handler you assign you can rethrow the exception, then catch it and print a more useful message.

Cylon Dinner Party
Dec 2, 2003

bored now.
Is there a prettier way to dereference this?

code:
int accept(vector <WSAPOLLFD> *fdarray)
{
    ...
    (*fdarray)[1].events = POLLWRNORM;

That Turkey Story
Mar 30, 2003

Cylon Dinner Party posted:

Is there a prettier way to dereference this?

code:
int accept(vector <WSAPOLLFD> *fdarray)
{
    ...
    (*fdarray)[1].events = POLLWRNORM;

What's wrong with that? If you're doing it a lot in one block you could always create a reference:

code:
int accept(vector <WSAPOLLFD> *fdarray)
{
  // Insert some assert or a check for null here
  // ...
  vector<WSAPOLLFD>& fdarray_ = *fdarray;
  fdarray_[1].events = POLLWRNORM;

Cylon Dinner Party
Dec 2, 2003

bored now.

That Turkey Story posted:

What's wrong with that? If you're doing it a lot in one block you could always create a reference:

Thank you! I find yours easier to read after I bury it in this:
code:
if (INVALID_SOCKET == ((*fdarray)[i].fd = accept(lsock,NULL,NULL)))

icantfindaname
Jul 1, 2008


Hey, I can't for the life of me figure out what's wrong with this program. It's supposed to take a two dimensional array and find the elements that are larger than their neighbors
code:
#include <stdio.h>

#define size 3

int IsLarger (int array[size + 2][size + 2], int i, int j);

main ()
{
	srand( (unsigned)time( NULL ) );

	int array[size+2][size+2], i, j;

	for (i = 0; i < size + 2; i++) {
		for (j = 0; j < size + 2; j++) {
			array[i][j] = -32766;
		}
	}

	for (i = 1; i <= size; i++) {
		for (j = 1; j <= size; j++) {
			printf("\nPlease enter an integer: ");
			scanf("%d", &array[i][j]);
		}
	}

	for (i = 1; i <= size; i++) {
		for(j = 1; j <= size; j++) {
			if (IsLarger(array, i, j) == 1) printf ("%d in location (%d, %d)\n", array[i][j], i, j);
		}
	}

	for (i = 1; i <= size; i++) {
		for(j = 1; j <= size; j++) {
			printf ("[%d]", array[i][j]);
		}
		printf("\n");
	}
}

int IsLarger (int array[size + 2][size + 2], int i, int j)
{
	int k, l;
	i--;
	j--;

	for (k = 2; k >= 0; k--) {
		for (l = 2; l >= 0; l--) {
			if (k && l) break;
			if (array[i + k][j + l] >= array[i][j]) return 0;
		}
	}

	return 1;
}

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
It's the line with the break statement. First, break; doesn't do what I think you think it does, and second, you have the condition wrong.

Jonnty
Aug 2, 2007

The enemy has become a flaming star!

Also, just don't use "l" as a variable if at all possible. My first thought when reading that was "why is he making one equal to two?"

pseudorandom name
May 6, 2007

Jonnty posted:

Also, just don't use "l" as a variable if at all possible. My first thought when reading that was "why is he making one equal to two?"

If your font doesn't distinguish 1, l and I, it is broken.

icantfindaname
Jul 1, 2008


rjmccall posted:

It's the line with the break statement. First, break; doesn't do what I think you think it does, and second, you have the condition wrong.

What condition?

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
Er. The condition on the line with the break statement.

Brecht
Nov 7, 2009
Other errors I see:

Don't use the word 'size' as a #define macro. At a minimum, uppercase it.

`main()' isn't valid as a declaration for your main function; `int main()' is.

Don't use magic numbers, like -32766.

icantfindaname
Jul 1, 2008


Alright, thanks, I got it to work now

icantfindaname fucked around with this message at 03:32 on Dec 22, 2010

icantfindaname
Jul 1, 2008


Whoops, actually it compiles but doesn't display the
code:
printf ("%d in location (%d, %d)\n", array[i][j], i, j);
part, as if IsLarger never returns true

Any more insight?

Here's the program as it is now
code:
#include <stdio.h>
#include <stdlib.h>

#define SIZE 3

int IsLarger (int array[SIZE + 2][SIZE + 2], int i, int j);

int main ()
{
	int array[SIZE+2][SIZE+2], i, j;

	for (i = 0; i < SIZE + 2; i++) {
		for (j = 0; j < SIZE + 2; j++) {
			array[i][j] = -32766;
		}
	}

	for (i = 1; i <= SIZE; i++) {
		for (j = 1; j <= SIZE; j++) {
			printf("\nPlease enter an integer: ");
			scanf("%d", &array[i][j]);
		}
	}

	for (i = 1; i <= SIZE; i++) {
		for(j = 1; j <= SIZE; j++) {
			if (IsLarger(array, i, j) == 1) printf ("%d in location (%d, %d)\n", array[i][j], i, j);
		}
	}

	for (i = 1; i <= SIZE; i++) {
		for(j = 1; j <= SIZE; j++) {
			printf ("[%d]", array[i][j]);
		}
		printf("\n");
	}
	system("pause");
}

int IsLarger (int array[SIZE + 2][SIZE + 2], int i, int j)
{
	int k, l;
	i--;
	j--;

	for (k = 2; k >= 0; k--)
	{
		for (l = 2; l >= 0; l--)
		{
			if ((k != 1) && (l != 1)) {
				if (array[i + k][j + l] >= array[i][j]) return 0;
			}
		}
	}
	return 1;
}

RichardA
Sep 1, 2006
.
Dinosaur Gum

icantfindaname posted:

code:
if ((k != 1) && (l != 1)) {
	if (array[i + k][j + l] >= array[i][j]) return 0;
}
What values can k and l have and can you run through these values without returning zero?

k != 0 || l != 0

RichardA fucked around with this message at 08:04 on Dec 22, 2010

icantfindaname
Jul 1, 2008


I think I have this figured out. How would I express the condition !(both k and l are equal to zero)? (!(k && l == 0)) doesn't seem to be working

boak
Aug 17, 2010

im gay

icantfindaname posted:

I think I have this figured out. How would I express the condition !(both k and l are equal to zero)? (!(k && l == 0)) doesn't seem to be working

!((k == 0) && (l == 0))

icantfindaname
Jul 1, 2008


Thanks, it finally works now

mr_jim
Oct 30, 2006

OUT OF THE DARK

boak posted:

!((k == 0) && (l == 0))

((k != 0) || (l != 0))

Standish
May 21, 2001

mr_jim posted:

((k != 0) || (l != 0))

code:
if (k || l)
it's idiomatic

Null Pointer
May 20, 2004

Oh no!

mr_jim posted:

((k != 0) || (l != 0))

Edit: beaten

mr_jim
Oct 30, 2006

OUT OF THE DARK

I know.

POKEMAN SAM
Jul 8, 2004

Standish posted:

code:
if (k || l)
it's idiomatic

code:
if (k | l)
:D

that awful man
Feb 18, 2007

YOSPOS, bitch

Brecht posted:

`main()' isn't valid as a declaration for your main function; `int main()' is.

Maybe he's going retro.

1294028
Apr 9, 2009

by T. Fine
I'm having some problems with pointers to pointers. I have a 2d array of chars that I want to pass to this function:
code:
void doShit(char **stuff, int width, int height)
{
    .....
}
However, when I try to pass the 2d array to the function like this:
code:
char stuff[9][9];
doShit(**stuff, 9, 9);
It gives me this warning:

quote:

warning: passing argument 1 of 'doShit' makes pointer from integer without a cast

What am I doing wrong?

1294028 fucked around with this message at 17:19 on Dec 24, 2010

Vanadium
Jan 8, 2005

You are dereferencing the array.

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.
It'll still be wrong after fixing that. A 2D array doesn't properly degrade to a pointer to a pointer: http://codepad.org/dHtivhoW . GCC, in its infinite wisdom, doesn't even warn about this by default.

All but the first dimension of a multidimensional array have to be specified in the function signature: http://codepad.org/gVyGz5BY

Dicky B
Mar 23, 2004

code:
template <unsigned int a, unsigned int b>
void DoShit(char stuff[a][b])
{
  ...
}
...
char stuff[9][9];
DoShit<9,9>(stuff);
Fart.

That Turkey Story
Mar 30, 2003

Dicky B posted:

code:
template <unsigned int a, unsigned int b>
void DoShit(char stuff[a][b])
{
  ...
}
...
char stuff[9][9];
DoShit<9,9>(stuff);
Fart.

This too, does not do what you think. The "a" there is meaningless except that it needlessly makes different values of "a" force new instantiations even though the generated function will be the same (the parameter type will be "char(*)[b]" no matter the [valid] value of "a"). What you probably meant was:

code:
template <unsigned int a, unsigned int b>
void DoShit(char (&stuff)[a][b])
{
  ...
}

...

char stuff[9][9];
DoShit(stuff);
Although for the poster's case, leaving out "a" entirely may also be acceptable.

You could also always use a custom datastructure such as Boost or tr1 array, or Boost.Multi_Array.

HIERARCHY OF WEEDZ
Aug 1, 2005

Does anyone know what std::_Tree is? I'm trying to compile the python bindings for Ogre on Linux, and the wrappers make reference to this, causing gcc to bork. From what I can tell it appears to be an MSVC-only helper class of some kind.

litghost
May 26, 2004
Builder

A OBLIVION MOD... posted:

Does anyone know what std::_Tree is? I'm trying to compile the python bindings for Ogre on Linux, and the wrappers make reference to this, causing gcc to bork. From what I can tell it appears to be an MSVC-only helper class of some kind.

std::_Tree is a utility classed used to implement std::set and std::map. What it likely means is you are using a library that was built against MSVC, and tried to build the new shared library using gcc. That will never work because of the differences in the C++ implementation's between MSVC and gcc. Whichever library brought in std::_Tree needs to be recompiled with gcc compiler (assuming that is your target compiler).

nielsm
Jun 1, 2009



litghost posted:

std::_Tree is a utility classed used to implement std::set and std::map. What it likely means is you are using a library that was built against MSVC, and tried to build the new shared library using gcc. That will never work because of the differences in the C++ implementation's between MSVC and gcc. Whichever library brought in std::_Tree needs to be recompiled with gcc compiler (assuming that is your target compiler).

If he's actually building on Linux, as I understand, then it won't be a case of libraries built with the wrong compiler (MSVC for Linux? Nah.), but source code depending on implementation details of the Dinkumware STL.

Check if there isn't somewhere you can configure something about the compiler or standard library being used, because it sounds like it's some wrong code being pulled in. Also look for std::_Tree in the Ogre source code, and if they actually do use that explicitly anywhere, write an angry mail to the developers telling them to stop relying on implementation details.

HIERARCHY OF WEEDZ
Aug 1, 2005

nielsm posted:

Also look for std::_Tree in the Ogre source code, and if they actually do use that explicitly anywhere, write an angry mail to the developers telling them to stop relying on implementation details.

This is exactly what's happening, albeit in the PythonOgre source code. They're using std::_Tree in the bindings themselves. I'm not interested in fixing their mistakes, so on to Ogre proper I go.

amr
Feb 28, 2005
I love boxes of toast
I don't know if anyone here is familiar with OpenCV, but I'm doing some basic facial detection then feeding that into facial recognition using the eigenfaces stuff that's built into OpenCV.

Seems simple enough, but I'm not getting great results and my eigenfaces look a bit... funny, the box seems to be too wide on the right and bottom edges:


Pretty thing, isn't she?

In my detection method I'm also resizing the image that's returned, so an issue could be there! I feel like I may be doing something exceptionally dumb, but I spent the entire day rewriting this thing from scratch so I may very well have missed something.

http://pastebin.com/PPR6Jk3A is my getFaceFromImage() function code

darkhand
Jan 18, 2010

This beard just won't do!
I can't really think of what to call them, but is there a widget used to make terminal-emulator-like windows? Things like xterm, cmd.exe, gVim.

What is that specific type of "text-area" called? A character frame buffer? Are there any GUI frameworks that have built-in widgets like that?

Vanadium
Jan 8, 2005

I think http://library.gnome.org/devel/vte/unstable/

darkhand
Jan 18, 2010

This beard just won't do!

Wow thanks!

Adbot
ADBOT LOVES YOU

amr
Feb 28, 2005
I love boxes of toast
Fixed it! I think. CvRect takes an (x,y) coordinate and a width and a height, not two (x,y) coordinates - oops.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply