LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 09-03-2006, 12:38 AM   #1
kornerr
Member
 
Registered: Dec 2004
Location: Russia, Siberia, Kemerovo
Distribution: Slackware
Posts: 893

Rep: Reputation: 35
How to add/remove certain element of vector?


Suppose I have vector<string> blah;
How to add/remove certain element of the vector? (By number)
Thanks.

Last edited by kornerr; 09-03-2006 at 12:41 AM.
 
Old 09-03-2006, 01:22 AM   #2
demon_vox
Member
 
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173

Rep: Reputation: 30
Very easy, you have an add(int, string) and a remove(int) method which will add and remove elements... But you should install Eclipse to program in Java, you wont have this question because Eclipse will assist you at the moment

Cheers
 
Old 09-03-2006, 02:25 AM   #3
Flesym
Member
 
Registered: Aug 2005
Location: Germany
Distribution: Ubuntu, Debian
Posts: 189

Rep: Reputation: 31
Quote:
Originally Posted by demon_vox
Very easy, you have an add(int, string) and a remove(int) method which will add and remove elements...
Well, if kornerr's question is about C++ and STL you neither have a "add-", nor a "remove" method. Instead they are called insert and erase. And the routines how to add and remove elements are not that easy, although they are not very hard.., let's say: It is different Because these mathods take iterators as positions (and no integers), you have to do it like so:
Code:
#include <iostream>
#include <vector>
#include <string>
using namespace std;

/**
 *Just a little helper function to print the array out.
 */
void vecOut(const vector<string> &v){
  cout << "Content:" << endl;
  for(int i = 0; i <v.size(); i++) 
    cout << v[i] << " ";
  cout << endl << endl;
}

int main()
{
  vector<string> v(7, "a");
  cout << "Size (original): " << v.size() << endl;
  vecOut(v);
	
  vector<string>::iterator vB = v.begin();
  int inPos = 2;
  v.insert(vB + inPos, "insert");
  cout << "Size (after insertion): " << v.size() << endl;
  vecOut(v);

  /***
    Important! - After resizing the array (insert, erase...), 
    all current iterators may become invalid! So re-assign them.
  ***/
  vB = v.begin();   
  v.erase(vB + inPos); 
  cout << "Size (after erase): " << v.size() << endl;
  vecOut(v);
  return 0;
}
Another thing is, that you should be very careful with these operations used on vectors, because they are quite expensive and will take you O(n) time! If you are working with a large number of elements or have to insert and erase very often, you should consider to switch to another container like a linked list.

PS:
Quote:
Originally Posted by demon_vox
But you should install Eclipse to program in Java, you wont have this question because Eclipse will assist you at the moment
Hmmm..., How does Eclipse help you with this problem? -I'm using Eclipse all day and it never assists me with such things O.o Instead, you should bookmark or download a documentation of your STL ==> E.g. here

Last edited by Flesym; 09-03-2006 at 02:29 AM.
 
Old 09-03-2006, 03:04 AM   #4
Mega Man X
Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 63
Quote:
Originally Posted by Flesym

PS:

Hmmm..., How does Eclipse help you with this problem? -I'm using Eclipse all day and it never assists me with such things O.o Instead, you should bookmark or download a documentation of your STL ==> E.g. here
I don't know if Eclipse will assist you with anything other then Java, but for example, if you write something like this:

Code:
	Vector someVector = new Vector();
		
	someVector.
As soon as you type the dot("."), Eclipse display a very nice drop down menu with all the possibilities to use after the "dot". I did a little screenshot for you:

http://goto.glocalnet.net/torch/temp...se/eclipse.png

It is very, very handy to when you are working with a lot of classes and packages and framework and etc. And this feature works really fast too. Alternatively, you can press "CTRL + Space" anywhere in your code, and eclipse will assist you again. Take this as an example:

http://goto.glocalnet.net/torch/temp...e/eclipse2.png

In this case, I made my MainClass a subclass of the JFrame, so my MainClass here is also a JFrame. While in the constructor, pressing CTRL + SPACE gave me a lot of options telling me what I could do with my JFrame subclass. Eclipse will also display methods that are deprecated for you.

It not only stops there. For example, if a method can throw an Exception, Eclipse will suggest you to put that line in a a "try-catch" body or add a "throws" at your method. It can also assist you to implement non-implemented methods. For example, let's say that my MainClass(a JFrame) also implements the Runnable interface. The Runnable interface has one method that MUST be implemented, the method "run()". Look at this screenshot:

http://goto.glocalnet.net/torch/temp...e/eclipse3.png

When something is wrong with your code, Eclipse will display a little red "X" at the beginning of the line with a problem. Pressing that little red X will give you some ideas of what is wrong. In this case, if you want, Eclipse can implement all missing methods for you (in this case, the method run()).

Another handy thing. Let's say you forgot to import javax.swing.JButton when creating a button. Pressing CTRL + Shift + O will import all missing libraries. Pressing CTRL + Shift + F will auto-organize your code. And there are billions of other useful things, like exporting your project as an executable jar file. If you are working with JavaEE/Webprogramming, you can start or stop, for example, Apache Tomcat from within the API, and a whole lot more ^_^

Whetter or not Eclipse will work this well with C++, I can't tell. For Java, Eclipse is really bad ass (<< sorry for the lack of a better word...hihi)

I hope this clarifies a little why Eclipse rocks ^_^;;

EDIT: Press CTRL + Shift + L for a list of Key assist in Eclipse or go the the Help >> Key assist on the menu bar

Last edited by Mega Man X; 09-03-2006 at 03:09 AM.
 
Old 09-03-2006, 03:43 AM   #5
Flesym
Member
 
Registered: Aug 2005
Location: Germany
Distribution: Ubuntu, Debian
Posts: 189

Rep: Reputation: 31
Yes, I know the code assistant features of Eclipse; and yes, they are much better implemented in Java than in C++, but none of them can help you, if you don't know what a method does and how to use it, or what method to take if you don't even know its name. These code assistant tools are only good (and very helpful), if you know what you are doing.

And yes, Eclipse rocks! -I never said anything else ;-)
 
Old 09-03-2006, 04:04 AM   #6
Mega Man X
Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 63
Quote:
Originally Posted by Flesym
Yes, I know the code assistant features of Eclipse; and yes, they are much better implemented in Java than in C++, but none of them can help you, if you don't know what a method does and how to use it, or what method to take if you don't even know its name. These code assistant tools are only good (and very helpful), if you know what you are doing.
That's true. If you don't know what an ArrayList is, for example, there's little use to learn the additional methods ^_^. In the beginning, when I started programming with Java, I had the API open all the time. Sometimes I still have to check it, but not that often, unless I'm adventuring in some uncharted waters (uncharted for me indeed ^_^).

Quote:
Originally Posted by Flesym
And yes, Eclipse rocks! -I never said anything else ;-)
Oh yes ^_^. I also used Eclipse a little bit with Python. It was nice ^_^.
 
Old 09-03-2006, 11:16 AM   #7
demon_vox
Member
 
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173

Rep: Reputation: 30
Hi,
I didnt know which language he was programming since it wasnt mentioned. Java was my first guess because of the generics (but not that its mentioned, C++ also uses templates with the same sintax, it was late for me at that moment :P )

But Eclipse does assist you. If you have a variable, say vec, when you type vec and press the point key ( . ) (or CTRL+SPACE) you will have a list with all the message that object understands. And of course add and remove are there. So at first, if you want to do something and you dont know how, you can check all the message that object understands right where you are typing the code.
And thats why I said Eclipse assist you in this kind of things.

Cheers!
 
Old 09-09-2006, 06:37 PM   #8
robbbert
Member
 
Registered: Oct 2005
Location: Hannover, Germany
Distribution: Let there be Ubuntu... :o)
Posts: 573

Rep: Reputation: 32
Context-sensitive documentation in the source code editor

Quote:
Originally Posted by Flesym
Yes, I know the code assistant features of Eclipse; and yes, they are much better implemented in Java than in C++, but none of them can help you, if you don't know what a method does and how to use it, or what method to take if you don't even know its name. These code assistant tools are only good (and very helpful), if you know what you are doing.
At Mega Man X's screenshots (i.e., this one) there are still two Eclipse features missing:

1. The method parameter names (i.e., "String arg0") can be more more descriptive (i.e., "String fileName").

2. There can be an automatic tooltip box, that displays the JavaDoc information on the methods and parameters as you select them.

Both features are becoming available when you've added the Java sources to the referenced jars or libraries:

1. In Eclipse, click Windows >> Preferences. In the left tree, select Java >> Installed JREs.
Edit one of them, select a Jar in the dialog that pops up from that, and click Source Attachment.

2. You can do the same thing by right-clicking any element in Eclipse's Java editor, and clicking Open Declaration. Another editor will open, and if there's no source code attached yet, there will be a button Attach Source.

BTW, there's a - context sensitive - JavaDoc Eclipse View, too, that is very useful.

- Code completion plus context-sensitive documentation is absolutely unbeatable.

HTH

Last edited by robbbert; 09-09-2006 at 06:40 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Add/Remove Programs rembot Ubuntu 21 08-27-2006 07:41 AM
point / STL vector, change element question true_atlantis Programming 1 09-17-2005 01:46 PM
How to add a local APIC vector entry? Dagda99 Programming 3 08-02-2004 06:45 PM
Cant add MySQL through add/remove programs Hero Doug Fedora 6 01-03-2004 10:35 PM
RH 9.0 Add/Remove Programs rosco Linux - Newbie 0 09-12-2003 05:05 PM


All times are GMT -5. The time now is 05:12 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration