The following is what I found compiled with C++. I hope it will be useful to you
TopCoder C++ for C programmers:
C++ is added based on the C language Object-oriented extension. C++ provides many advantages that the traditional C language does not have while maintaining compatibility with the C language, so that people can use C and C++ simultaneously in one program. During the competition, you must use some basic C++ functions to answer questions. Below we briefly introduce some C++ knowledge that programmers who are familiar with C language must know to participate in the competition.
Classes and Methods
The most important new concept added in C++ is the class. Classes can contain methods and variables. When competing, you must write a class that contains at least one method. As an example, consider the following problem definition:
Class: CellTower
Method: best
Parameters: vector Return: int Method signature: int best(vector <:string> towers, int x, int y) To answer this question you need to create a name Class for CellTower. This class must contain a method named best. The method best must have three parameters (vector Classes are defined using the class keyword. Defining a class is similar in syntax to defining a C structure. To define the class required by the above question, you can use the following code: class CellTower { public: int best(vector //Your code } }; How and how the method is defined A C function is similar. The keyword public tells the compiler that the method we define can be called by any object. Only then can the test program run your code correctly. STL Many classes and functions often used in competitions come from the Standard Template Library, usually abbreviated as STL in English. STL provides a common set of libraries that contain tools ranging from basic character processing to complex sorting algorithms. To participate in the competition you must be familiar with at least two classes: vector and string. Includes Before you use a class in STL, you must include the corresponding header file in your code. The vector class comes from the header file "vector"; the string class comes from "string". You also need to add the following line to your code: This line tells the compiler to look for classes in the std namespace. Vector Vector is used in C++ to replace C arrays. Vector solves many problems of traditional C language arrays. Vector can query the current array size and dynamically adjust the size. A vector can be declared using vector vector The size of the newly created vector is 0. To declare a vector of another size, you can use: vector In this example the newly created vector has a size of 10. To set or read an item in a vector, you can use a syntax similar to that used for arrays in C: vector myVar[0] = 1; //Set the item at position 0 to 1 printf("%i", myVar[0]); //Output 1
The size() method returns the current size of the vector:
for(int i = 0; i < myVar.size(); i++) {
printf("%i", myVar [i]); //Output the i-th item
}
Use the resize method to resize the vector:
myVar.resize(15); //The size of myvar becomes 15
The vector class provides many other useful functions. Click the reference link below to learn more about vector.
String (string)
The string class is used to replace char* to express strings. string provides basic character processing and provides a way for many functions that use char* to still use string. You can use the = and + operators to assign values ??to strings:
string s;
s = "Hello";
s = s + ", world" ;
s starts out as an empty string (size 0). In the second line the value of s changes to "Hello". Then ", world" is added to the end of s, and the value of s becomes "Hello, world".
The method size() returns the length of the string. These codes:
string s = "Hello";
printf("%i", s.size());
will output 5.
You can access a character in a string just like a char array:
string s = "Hello";
printf("%c", s[0]); //Output "H"
s[0] = 'h'; //s becomes "hello"
If a function requires char* Parameters, you can use the c_str() method:
string s = "Hello";
printf("%s", s.c_str()); //Output "Hello"
Answer Example
Look at the following example question:
Our input program will record keyboard input into a sequence of characters. We want to convert the input character sequence into a separate string for later use. Create a method buildstring. This method accepts a vector type parameter. This parameter represents the keyboard input sequence. The buildstring method must return a string merged from this input sequence.
Class: StringBuilder
Method: buildString
Parameters: vector
Return: string
Method signature: string buildString(vector
The following answer uses string and vector to solve this problem:
#include
#include
using namespace std;
class StringBuilder {
public:
string buildString(vector
//Create the string to be returned
string s;
//Loop to read each item in the array
for(int i = 0; i < array.size(); i++) {
s = s + array[i];
}
//Return the created string
return s;
}
};