Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ vector equivalent in C [duplicate]

Tags:

c++

c

vector

I have a code (C++) that looks like this

vector<int> values[10000];

int i, j; 
while (.....) {
    scanf("%d%d", &i, &j);
    values[i].push_back(j);
    values[j].push_back(i);
}

but I want to rewrite this code to C. How can I do this?

I researched the opportunity to make the own stack, but maybe have more lightweight way to rewrite this code, maybe two-dimensional arrays. So far I can not think how this remake, I hope that someone more experienced tell me how to do it :)

Sorry guys, added a more advanced example...

like image 323
Alex Avatar asked Dec 29 '12 17:12

Alex


People also ask

What is equivalent of vector in C?

A rough equivalent of a C++ vector would be a resizing C array (to account for more elements than available). Ergo, the equivalent of an array of vectors would be an array of pointers (an array of arrays wouldn't cut it because of the resizing constraint). int* values[1000];

Can I use vector in C?

Vectors are a modern programming concept, which, unfortunately, aren't built into the standard C library. Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container.

Does Push_back make a copy?

Yes, std::vector<T>::push_back() creates a copy of the argument and stores it in the vector.

Is there a vector library in C?

C Vector Library A simple vector library for C. This libary's vectors work in a similar manner to C++ vectors: they can store any type, their elements can be accessed via the [] operator, and elements may be added or removed with simple library calls. Note: the vector type is just an alias for void* defined in vec.


1 Answers

Instead of rolling your own, you may want to try a C container library, e.g. http://code.google.com/p/ccl/

like image 144
holtavolt Avatar answered Sep 28 '22 17:09

holtavolt