Sponsored Content
Full Discussion: sort a vector
Top Forums Programming sort a vector Post 302501698 by homeboy on Friday 4th of March 2011 09:54:56 AM
Old 03-04-2011
sort a vector

Hi all,
I have a vector,the type of the element within it is list<int>,and i wanna sort this vector.So i implemented a function as a predicate for sort(the STL algorithm).Problem came when i missed the bold part in my code,g++ generated lots of error messages.And after i added the bold part,the code compiled successfully.Why?(As i know,the predicate need not to have a const parameter)Any tips will be appreciated.Thanks.
Dengjin.
Code:
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <list>

using namespace std;

void print(list<int>& lst)
{
	copy(lst.begin(),lst.end(),
			ostream_iterator<int>(cout," "));
	cout<<endl;
}

bool sortCritic(const list<int>& lst1,const list<int>& lst2)
{
	return lexicographical_compare(lst1.begin(),lst1.end(),
											 lst2.begin(),lst2.end());
}

int main()
{
	list<int> c1,c2,c3,c4;
	
	for(int i=1;i<=5;i++)
		c1.insert(c1.end(),i);

	c4 = c3 = c2 = c1;

	c1.push_back(7);	
	c3.push_back(2);
	c3.push_back(0);
	c4.push_back(2);

	vector<list<int> > cc;

	cc.push_back(c1);
	cc.push_back(c2);
	cc.push_back(c3);
	cc.push_back(c4);

	for_each(cc.begin(),cc.end(),print);
	sort(cc.begin(),cc.end(),sortCritic);
	cout<<endl;
	for_each(cc.begin(),cc.end(),print);
	
}

 

10 More Discussions You Might Find Interesting

1. Programming

Saving a vector to a file

Another C++ question, please do notify me if this forum is C-only! :) I'm having a vector... vector<Person> x; ... with a class I've defined (Person). I have several elements in this vector and I would like to save it to a file (binary (?)). And later on open up this file to retrieve... (1 Reply)
Discussion started by: J.P
1 Replies

2. Programming

Linker error when using vector's

using SUN C++ I have a problem when I do a push_back on a vector. The linker gives me a undefined symbol error on __cxa_end_catch. Any ideas, is there a library to include? Thanks Chris (1 Reply)
Discussion started by: CDurkin
1 Replies

3. Programming

Vector Traversing

Hi i have the following structure struct S { char Mod_num; char val; char chr_nm_cd; } I am reading a 2GB file and inserting into the structure and writing into a vector. I feel like only vector will be a right option. I tried with multimap but it is memory intensive and hence i... (1 Reply)
Discussion started by: dhanamurthy
1 Replies

4. Programming

multidimensional array using c++ vector

Hi! I need to make dynamic multidimensional arrays using the vector class. I found in this page How to dynamically create a two dimensional array? - Microsoft: Visual C++ FAQ - Tek-Tips the way to do it in 2D, and now i'm trying to expand it to 3D but i don't understand how is the operator working,... (0 Replies)
Discussion started by: carl.alv
0 Replies

5. Programming

c++ mutidimentional arrays using vector

Hi! I need to make dynamic multidimensional arrays using the vector class. I found in this page How to dynamically create a two dimensional array? - Microsoft: Visual C++ FAQ - Tek-Tips the way to do it in 2D, and now i'm trying to expand it to 3D but i don't understand how is the operator working,... (1 Reply)
Discussion started by: carl.alv
1 Replies

6. Programming

C++ Map using a Vector as a Value Type?

I modified some code I found on Wikipedia concerning maps to see if it would work before applying it to a project I'm working on that has a similar idea. What I would want is for a user to be able to enter sentences and then be able to distinguish how many times a the person entered a word in a... (4 Replies)
Discussion started by: kcgb20
4 Replies

7. Programming

Array and Vector

Hi all, from my understanding I understand that I can use array in this manner. struct test { int a; int b; int c; }; test testing; //creating an array with the structer type testing.a=1; testing.b=2; testing.c=3; If I'm not wrong we can use array in this manner,... (12 Replies)
Discussion started by: vinzping
12 Replies

8. Programming

vector c++

hello guys. i'm new to c++. i've problem using two dimensional vector. i've a project of making conway's game of life. this is the code that i have made so far. my problem is how can i give a two dimensional vector through main. glider.vec1 = vec; is not correct way to give a two... (2 Replies)
Discussion started by: nishrestha
2 Replies

9. Shell Programming and Scripting

Vector normalisation

In AWK For 3 individual vectors of the form: -2.772 -9.341 -2.857 -5.140 -6.597 -1.823 -2.730 -5.615 1.159 I would like to write a script that parses line by line to (i) normalise, (ii) divide by the norm for *each* vector. I.e. sqrt(-2.772^2 + -9.341^2 + -2.857^2)=10.154 ... (4 Replies)
Discussion started by: chrisjorg
4 Replies

10. Programming

Number to bit vector

Is there a function to convert number (unsigned int for this example) to binary? I could not find a simple one thru google. While I was learning bloom filter with the example, I was wondering if anybody can help me to 1) display the real bits vector for the bloomfilter; 2) if dataset is very... (11 Replies)
Discussion started by: yifangt
11 Replies
KCPUSET(9)						   BSD Kernel Developer's Manual						KCPUSET(9)

NAME
kcpuset, kcpuset_create, kcpuset_destroy, kcpuset_copy, kcpuset_use, kcpuset_unuse, kcpuset_copyin, kcpuset_copyout, kcpuset_zero, kcpuset_fill, kcpuset_set, kcpuset_clear, kcpuset_isset, kcpuset_iszero, kcpuset_match, kcpuset_merge, kcpuset_atomic_set, kcpuset_atomic_clear -- dynamic kernel CPU sets SYNOPSIS
#include <sys/kcpuset.h> void kcpuset_create(kcpuset_t **retkcp, bool zero); void kcpuset_destroy(kcpuset_t *kcp); void kcpuset_copy(kcpuset_t *dkcp, kcpuset_t *skcp); void kcpuset_use(kcpuset_t *kcp); void kcpuset_unuse(kcpuset_t *kcp, kcpuset_t **lst); int kcpuset_copyin(const cpuset_t *ucp, kcpuset_t *kcp, size_t len); int kcpuset_copyout(kcpuset_t *kcp, cpuset_t *ucp, size_t len); void kcpuset_zero(kcpuset_t *kcp); void kcpuset_fill(kcpuset_t *kcp); void kcpuset_set(kcpuset_t *kcp, cpuid_t cpu); void kcpuset_clear(kcpuset_t *kcp, cpuid_t cpu); int kcpuset_isset(kcpuset_t * kcp, cpuid_t cpu); bool kcpuset_iszero(kcpuset_t *kcp); bool kcpuset_match(const kcpuset_t *kcp1, const kcpuset_t *kcp2); void kcpuset_merge(kcpuset_t *kcp1, kcpuset_t *kcp2); void kcpuset_atomic_set(kcpuset_t *kcp, cpuid_t cpu); void kcpuset_atomic_clear(kcpuset_t *kcp, cpuid_t cpu); DESCRIPTION
The machine-independent kcpuset subsystem provides support for dynamic processor sets. Conceptually kcpuset can be understood to be the ker- nel equivalent of the user space cpuset(3) interface. FUNCTIONS
kcpuset_create(retkcp, zero) The kcpuset_create() function creates a dynamic CPU set and stores the result to retkcp. If the boolean zero is not false, the allocated set is also initialized to zero. kcpuset_destroy(kcp) Destroys the CPU set kcp and schedules any linked CPU sets for deferred destruction. kcpuset_copy(dkcp, skcp) Copies the CPU set pointed by skcp to dkcp. kcpuset_use(kcp) Marks kcp as being in use by increasing the reference count of the object. Note that initially kcpuset_create() sets the reference count to 1. kcpuset_unuse(kcp, lst) Decreases the internal reference count of kcp, and on the last reference (when the count reaches zero), destroys kcp. If lst is not NULL, then instead of destroying, kcp will be added to the lst list for a deferred destruction. kcpuset_copyin(ucp, kcp, len) Copies the len bytes long user-space CPU set ucp to the kernel CPU set kcp. kcpuset_copyout(kcp, ucp, len) Copies the kernel CPU set kcp to the user-space CPU set ucp. kcpuset_zero(kcp) Clears the set kcp. kcpuset_fill(kcp) Fills the whole set kcp with ones. kcpuset_set(kcp, cpu) Adds cpu to the set kcp. kcpuset_clear(kcp, cpu) Removes cpu from the set kcp. kcpuset_isset(kcp, cpu) Returns 1 if cpu is part of the CPU set kcp. kcpuset_iszero(kcp) Returns true if the set kcp is empty. kcpuset_match(kcp1, kcp2) Compares the sets kcp1 and kcp2, returning true if these are identical. kcpuset_merge(kcp1, kcp2) Merges the set kcp2 to the set kcp1. kcpuset_atomic_set(kcp, cpu) The kcpuset_atomic_set() function operates as kcpuset_set(), but the operation is atomic; see atomic_ops(3) for more details. kcpuset_atomic_clear(kcp, cpu) Removes cpu from the CPU set kcp atomically. CODE REFERENCES
The kcpuset subsystem is implemented within sys/kern/subr_kcpuset.c. SEE ALSO
cpuset(3) HISTORY
The kcpuset subsystem first appeared in NetBSD 6.0. BSD
October 6, 2011 BSD
All times are GMT -4. The time now is 11:43 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy