Jumat, 14 Desember 2012

ANSI C++ string class and the C standard library:

he full use of the C standard library is available for use by utilizing the ".c_str" function return of the string class.

01#include <strings.h>
02#include <string>
03#include <stdio.h>
04using namespace std;
05    
06int main()
07{
08   char *phrase1="phrase";
09   string phrase2("Second phrase");
10   char  phraseA[128];
11   char  *phraseB;
12    
13   strcpy(phraseA,phrase2.c_str());
14   phraseB = strstr(phrase2.c_str(),phrase1);
15 
16   printf("phraseA: %s\n",phraseA);
17   printf("phraseB: %s\n",phraseB);
18   printf("phrase2: %s\n",phrase2.c_str());
19}  
Compile and run:
[prompt]$ g++ test.cpp
[prompt]$ ./a.out
phraseA: Second phrase
phraseB: phrase
phrase2: Second phrase

Iterator types:

  • string::traits_type
  • string::value_type
  • string::size_type
  • string::difference_type
  • string::reference
  • string::const_reference
  • string::pointer
  • string::const_pointer
  • string::iterator
  • string::const_iterator
  • string::reverse_iterator
  • string::const_reverse_iterator
  • string::npos


ANSI C++ string class iterators:

terators provide the ability to access the individual characters in a string.
01#include <iostream>
02#include <string>
03using namespace std;
04    
05int main()
06{
07   string alphabetLC="abcdefghijklmnopqrstuvwxyz";
08 
09   string::const_iterator cii;
10   int ii;
11 
12   for(cii=alphabetLC.begin(); cii!=alphabetLC.end(); cii++)
13   {
14      cout << ii++ << " " << *cii << endl;
15   }
16}
This will print the integer position in the string followed by the letter for all characters in the alphabet.
0 a
1 b
2 c
3 d
4 e
5 f
6 g
7 h
...
..


STL C++ string functions:

Assuming declaration: string Var;
Function/Operation Description
Var = string2
Var.assign("string-to-assign")
Assignment of value to string. When assigning a C "char" data type, first check if NULL to avoid failure/crash.
i.e.: if( szVar ) sVar.assign( szVar );
where szVar is a C "char *" data type and sVar is of type "string".
Var.swap(string2)
swap(string1,string2)
Swap with value held in string2.
Function swap will exchange contents of two string class variables.
Var += string2
Var.append()
Var.push_back()
Append string/characters.
Var.insert() Insert characters
Var.erase()
Var = ""
Clear string variable. No arguments necessary.
+ Concatenate
==, !=, <, <=, >, >= Compare strings.
Var.compare(string)
Var.compare( size_t pos1, size_t len, string ) const;
Var.compare( size_t pos1, size_t len1, const string, size_t pos2, size_t len2 ) const;
Compare strings. Returns int:
  • 0: if equal.
  • -1: Not equal. 1st non matching character in Var is less in value based on ASCII table than in compare string.
  • +1: Not equal. 1st non matching character is greater in value based on ASCII table.
Where string is another STL string or null terminated C string.
Var.length() Return length of string. No arguments necessary. The methods length(), size() and capacity() all return the same value.
Var.size() Return length of string. No arguments necessary.
Var.capacity() Return length of string + 1. Red Hat 7.x. Red Hat 8.0+ returns the number of characters without the "+1". Number of characters that can be held without re-allocation.
No arguments necessary.
Var.max_size() Returns a very large number. No arguments necessary.
Var.empty() Returns 1 if an empty string.
Returns 0 if not empty.
<< Output stream
>>
getline()
Input stream
Var.c_str() Returns C string pointer. C char string is null terminated. Do not free memory using this pointer!
Var.data() Returns C string pointer. C char string is NOT null terminated. Do not free memory using this pointer!
Var[]
Var.at(integer)
Access individual characters. Return single character at specified position (integer).
Var.find(string)
Var.find(string, positionFirstChar)
Var.find(string, positionFirstChar, len)
Find first occurance of string or substring. Returns int position of first occurance in string. Where len is the length of the sequence to search for.
Returns string::npos if not found.
i.e. if(Var.find("abc") == string::npos) cout << "Not found" << endl;
Var.rfind() Find last occurance of string or substring.
Var.find_first_of(string, position)
Var.find_first_of( string, size_t position, size_t len )
Find strings and substrings.
Where string is another STL string or null terminated C string.
If position = 0, than start at beginning of string.
Var.find_last_of() Find strings and substrings.
Var.find_first_not_of()
Var.find_last_not_of()
Find strings and substrings.
Var.replace(pos1, len1, string)
Var.replace(itterator1, itterator2, const string)
Var.replace(pos1, len1, string, pos2, len2)
Replace section of string with new characters.
pos2 and len2 are given when using only a substring of string. Where string is another STL string or null terminated C string.
Var.substr(pos, len) Return substring of text given a start position in string object and length.
Var.begin()
Var.end()
Iterators
Var.rbegin()
Var.rend()
Reverse iterators
Note that in most cases the string functions have been overloaded to accept both string class arguments and C char variables.

Creating Quick Rollovers and Flash Buttons using Dreamweaver

A word of caution!

Some older browsers do not display Flash Text or Flash Buttons. If the audience you are writing for uses mainly older browsers or mobile browsers you might not want to include too many examples of Flash elements at first. Another good idea, would be to provide text links in addition to Flash links. Have fun, but don't go overboard.

Move your mouse pointer to the text below. The text turns purple and your mouse pointer turns to a hand.
The text is not a link, but it could easily be. This page describes how to create such rollovers using Dreamweaver.

Flash Text

The title of this web page ( Using Dreamweaver to Create Quick Rollovers ) was created using Flash Text. Although the text was typed, it behaves as if it is an image.
To create a Flash Text image go to the Insert menu. Choose Interactive Images then Flash Text .
Several options are available to you; text color, rollover color, size and style, font and link or target. The Flash Text image below will be used in this example.

The window below shows that the text was written in Abadi MT Condensed 20 point. The text color selected was black (#000000) with purple (#CC00FF) as the rollover color. The text is a link to Internet4Classrooms home page. If you follow the link, use the back button to come back to this page.
The first few times you do this you might overlook naming the image. A default name will be used with the extension .swf if you do overlook this.
One way to see the results of your work is to save the page and view it in a browser. However, Dreamweaver allows a quicker way to view the results. Select the text you inserted onto your page and look at the properties window. Select the Play button on the bottom right of the Properties window and move your cursor over the Flash Text to observe the color change. When you are finished click the same button, which now says Stop . Flash Buttons Sample buttons available in Dreamweaver

None of these buttons act as links. However, that is only because link information was not entered.

Dreamweaver allows you to quickly create rollover buttons in much the same way that you created Flash Text above. The Insert Flash Button window below, looks quite similar to the Flash Text window at the top of this page.
The button immediately below this text is the one that you may have used to come to this page on Rollover effects in Dreamweaver.
The next step is for you to create your own rollovers with Dreamweaver.
A word of caution! Some older browsers do not display Flash Text or Flash Buttons. If the audience you are writing for uses mainly older browsers you might not want to include too many examples of Flash elements at first. Another good idea, would be to provide text links in addition to Flash links. Have fun, but don't go overboard.

Go To Previous Tutorial: Dreamweaver Links

Go To Next Tutorial: Easy JavaScript with Dreamweaver

Writing Hyperlinks in Dreamweaver

Hyperlinks can be written to allow a viewer of your page to move to some other location.
Images or text can be used to include the hyperlink information.
Several types of locations will be discussed below:
A web page not on the server you are using (an external page) - Your first step is to visit the page you want to make a hyperlink to. Click into the URL in the Address or Location block. The URL should highlight. If it does not, click and drag to highlight the entire URL. Copy the URL. Never try to type in a URL, even one as simple as http://www.cnn.con (did you catch it, all it takes is one keystroke error and your hyperlink will not work). Go back to the page you are working on in Dreamweaver and select the object (text or image) which you will use for the hyperlink. With the text or image selected, paste the URL into the Link portion of the properties window, as in the example below:
A specific location on an external page - A page of Earth Science Internet links has an index at the top of the page. That index is a set of hyperlinks which point to invisible anchors. Suppose you were preparing a lesson and wanted to send your students to the part of the page that has weather links. Don't send them to the top of the page and trust that they will click on the right part of the index. Select the link yourself. Notice that the URL now has something added after the file name. That is the hyperlink to the invisible anchor. For example:
http://www.internet4classrooms.com/earthspace.htm
becomes
http://www.internet4classrooms.com/earthspace.htm#weather
When you find a URL with a link to an anchor, copy the entire URL and follow the same procedure outlined above.
A web page located in your own directory of pages - To make this hyperlink you do not need the entire URL, only the file name. From the example above, that would be earthspace.htm. Do not type this hyperlink in the Link portion of the properties window. If you leave off the extension, or include the wrong extension (.html when it should have been .htm, for example) the link will not work. Dreamweaver has a simple method for writing this kind of hyperlink. Select the object (text or image) which you will use for the hyperlink. With the text or image selected, click on the small folder to the right of the Link portion of the properties window.
When you click on the folder, you will be presented with a select file window. Select the file that you wish to make a hyperlink to.
A specific location on the web page you are working on - An invisible object in Netscape Composer is called a target. Personally, I think that is a better name than Dreamweaver's term, anchor. The same sort of thing in MS Word is called a bookmark. They all do the same thing, they provide a location for a hyperlink to point to. As an example, I inserted invisible anchors before each of the bold type lines describing the type of hyperlink. Then I made hyperlinks of each of the four bulleted items at the top of the page.
Step 1- Insert the invisible named anchor. Place your cursor where you want the anchor to be. From the Insert menu select Invisible Tags then select Named Anchor.
Step 2 - Name the anchor. Although the anchor is invisible, the name becomes part of the URL. Put some thought into a naming system for anchors on a page. Since this may become a part of the URL, remember to use no spaces.
Step 3 - Write the link to your anchor. Select the object (text or image) which you will use for the hyperlink. With the text or image selected, type # and the anchor name. Don't put spaces anywhere in the link name, especially not after the number sign (#). Select this link if you want to see what the URL pointing to an anchor looks like.
Using an image as a hyperlink - Look at the bottom of this page. The word WebGuide is an image, and it is a link to the WebGuide template. One important consideration is seen in the image below.
If you don't make the image border zero, there will be a box around the image, indicating that it is a link. If you like the way that looks, leave the border at 1 or 2.
A blank Email message ready to go to yourself - If you maintain a web site you will want to make it easy for someone to get in touch with you. Notice that below this section I have invited readers who have questions to send Email. Place your cursor on the purple word Email and you will see how this is done. To make a mail link, highlight the words, or picture, that you want to serve as a link and in the link portion of the Properties window enter:
mailto:you@youraddress.whatever
That is: mailto: followed by your exact email address. There must be no spaces. You can type your email address and then highlight it and enter the mailto link but that is usually a waste of space. If you disagree, send me Email.
This procedure also works in the body of an Email message. If you wish the recipient to reply to some other address than the one in the Reply to: field, write the link the just as you would in the link section of the Properties window
A new window, leaving the original browser window open - To leave the original page open and allow a link to open a second browser window, select _blank in the Target block of the Properties window. If you have Dreamweaver open, look at the Properties window. The Target block should be gray. It does not become available until you make an entry into the Link block. Compare the picture of a Properties window above to your own Properties window.
Changing the color of hyperlinks - There are two possible answers to this; changing the color of all links on a page, or changing the color of a specific link.
  1. To specify the link color for all links on a page go to the Modify menu and select Page Properties. There you will find the option to select text color for a page, link color, visited link color, and active link color. Visited link color takes effect when someone viewing your page returns to the page after visiting a link. Active link color is applied as someone clicks on the link to go to another page and when they return to the page but before they select another link.
  2. To change the color of one link is another matter. The link you followed to get to this part of our page has thirteen different colors. Please be sure that you notice the last word in that link string. That is the real key to changing link color. Do not change the color of text before writing the link. Write the link as you normally would, letting the default link color apply. Beginning with the first letter of the words in your hyperlink, highlight all but the last letter. Go to the text color box and change the color of the highlighted text. The link should look something like this; this is the link. [Caution, this is not a link it just looks like one.] If you include the last letter Dreamweaver will not change the link color when your page is viewed on the web. Next, click into the changed color portion of the link, just before the last letter and add that letter. It will be the same color as the rest of the changed hyperlink. It will look like this - this is the linkk. Now, how do you get rid of that final letter, which is the default color? That's right, use your Delete key. This drove me crazy until I learned that trick about the final letter.

Using Tables on Your Web Page

able Basics

- To review table basics, Return to Tutorial on Dreamweaver Basics .

Color Background in a Cell

- With a table selected, the Properties window can be expanded to the view you see below. Cell properties are changed in the bottom of the window. To the right of Header is Bg followed by a gray box with a down arrow. Click anywhere in that box for a color palette. Dreamweaver displays the hex code for that color. One step further to the right is a Brdr box. Dreamweaver will place a colored border around the inside of the table cell.
Those who view your page with Netscape will not see the border, Internet Explorer viewers will see it. As an example look at the table below the title of this page. IE viewers will see a purple border.

Invisible Tables

- Giving a table a border of 0 (zero), will make the lines around cells invisible. However, the table retains its basic structure, allowing you to organize text on a web page. The table below is two rows deep and three columns wide.
Organize blocks of text in a table with an invisible border. You can not use the tab to move over in web page development software. However, invisible borders can give the illusion of having used tabs
Hi Hi Hi

Changing Span

- Table cells are not constrained to be the same height or width. A single cell can spread down to cover several rows, or spread across to cover several columns. This spreading is known as Spanning. Cells may also be split. To change span or split right click in a cell. (Yes, Mac users, you too can right click. Control + click) The following pop up box is displayed:
This cell spans down to cover three rows This cell spans right to cover two rows
Hi
Hi Hi Hi Hi Hi Hi Hi Hi
The cell to the right was split into two cells Hi
Hi

Image as Background

- A single background image may be used for the entire table, or each cell may contain separate images. However, you must be very careful about using images behind text. Keep in mind the reason you wrote the web page. Don't do things that will make your message difficult to read. The Properties window is used to place a background image in the table or cell. Notice in the Properties window below that the upper background box contains the image information. Browse for an image by clicking on the folder icon.