I have been working with VB for a while now. Now I'm giving C++ a shot, i have came across strings, i cant seem to find a way to declare a string.
For example in VB:
Dim Something As String = "Some text"
Or
Dim Something As String = ListBox1.SelectedItem
Whats the equivalent to the code above in C++ ?
Any help is appreciated.
C++ supplies a string class that can be used like this:
#include <string>
#include <iostream>
int main() {
std::string Something = "Some text";
std::cout << Something << std::endl;
}
using the standard <string> header
std::string Something = "Some Text";
http://www.dreamincode.net/forums/topic/42209-c-strings/
In C++ you can declare a string like this:
#include <string>
using namespace std;
int main()
{
string str1("argue2000"); //define a string and Initialize str1 with "argue2000"
string str2 = "argue2000"; // define a string and assign str2 with "argue2000"
string str3; //just declare a string, it has no value
return 1;
}
Preferred string type in C++ is string, defined in namespace std, in header <string> and you can initialize it like this for example:
#include <string>
int main()
{
std::string str1("Some text");
std::string str2 = "Some text";
}
More about it you can find here and here.
In C++, strings' are not primitive data types. Therefore, we have to #include the string class OR use the std namespace. Here is an example of using namespace.
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello.\n"<<"What is your name?\n";
string name;
cin>>name;
cout<<"Hello "<<name<<"!";
}
Related
I tried to write a program that takes input as a string and then passes that string to a macro which is supposed to insert the string as a plain-text expression but the macro is not behaving as I would suspect.
#include <iostream>
#include <cmath>
#include <string>
#define PARSE(a) a;
using namespace std;
int main() {
string c ;
int b;
cin >> c;
b = PARSE(c);
cout << b;
return 0;
}
This code will not compile, it gives an error saying that I cannot convert string to int, however PARSE(c) should not be treated like a string it should just be replaced by plain text.
The error means you are really trying to convert std::string (c) to int and it can't be done (b).
If you want to access the plain text, once you are using std::string, you should call the c_str() method, example:
cout << c.c_str();
#edit:
If you are trying to do something like "eval()" from PHP, you can check this: https://stackoverflow.com/a/11078610/12385171
I am trying to parse a std::string that might contain Chinese characters. For example for a string contains
哈囉hi你好hello
I want to separate them into 6 strings:哈, 囉, hi, 你, 好, hello. Right now the string is obtained by using getline() from a text file. Referencing this post How to use boost::spirit to parse UTF-8?, here's my current code:
#include <boost/regex/pending/unicode_iterator.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/range.hpp>
#include <iterator>
#include <iostream>
#include <ostream>
#include <cstdint>
#include <string>
using namespace boost;
using namespace std;
using namespace std::string_literals;
int main()
{
string str = u8"哈囉hi你好hello"; //actually got from getline()
auto &&utf8_text = str;
u8_to_u32_iterator<const char*>
tbegin(begin(utf8_text)), tend(end(utf8_text));
vector<uint32_t> result;
spirit::qi::parse(tbegin, tend, *spirit::standard_wide::char_, result);
for(auto &&code_point : result) {
cout << code_point << ";";
}
}
But got the error: call to 'begin' and 'end' is ambiguous.
It works when I directly declare auto &&utf8_text = u8"哈囉hi你好hello", but I cannot write in this way because the content of string is determined by getline().
I also tried this:
auto str = u8"你好,世界!";
auto &&utf8_text = str;
but still got error: no matching function for call to 'begin' and 'end'.
auto with string literals results in a char pointer. If you want std::string, you have to write it out.
I am confused about the use of #include <string> at the start of a program. For example, in the code below, I don't use #include <string> but the function will still print out the string "Johnny's favorite number is" when it is run.
#include <iostream>
using namespace std;
void printVariable(int number){
cout << "Johnny's favorite number is" << number << endl
}
However, in this code below, it does contain #include <string>.
#include <iostream>
#include <string>
using namespace std;
class Var{
public:
void setName(string x){
name = x;
}
string getName(){
return name;
}
private:
string name;
};
int main(){
Var Classy;
Classy.setName("Johnny Bravo");
cout << Classy.getName() << endl;
return 0;
}
Do I only use #include <string> if a variable represents a string?
Do I only use #include <string> if a variable represents a string?
Yes.
Use #include <string> when you use a variable that has type std::string.
The code "text here", contrary to intuition, is not a std::string; it is a string literal, and a C-style string, and a const char[10] convertible to const char*. Welcome to C++ with its legacy oddities.
Your question arises from the fact that you know that something like "aabcd" is a string literal. So, its type should be string. Well, that's not quite true.
C++ has a lot of features from C. Including data types. So, that is a pointer to char (char*), not a string(an instance of the string class). You can create an instance of the string class from a char* (including a string literal) by passing it as argument to the constructor of string. But it is not a string, it's just some misleading terminology.
A similar case is calling things vectors when they are arrays.
If you use the type std::string in your code then you should include the <string> header. There are also a few other types and functions in that header, but std::string is the most commonly used one.
However, you do not need to include this header just to use string literals, which are built into the core language.
In your first case, library "string" is not needed. The object "cout" is supported by library "iostream", thus you have:
#include <iostream>
For the second case, you do explicitly use "string", thus library "string" is required:
#include <string>
I can't declare a string in my program:
string MessageBoxText = CharNameTextBox->Text;
it just doesn't work. It says string is undeclared identifier. What am I missing in the namespace or include or something like that?
Make sure you've included this header:
#include <string>
And then use std::string instead of string. It is because string is defined in std namespace.
And don't write this at namespace scope:
using namespace std; //bad practice if you write this at namespace scope
However, writing it at function scope is not that bad. But the best is one which I suggested before:
Use std::string as:
std::string MessageBoxText = CharNameTextBox->Text;
To use the standard string class in C++ you need to #include <string>. Once you've added the #include directive string will be defined in the std namespace and you can refer to it as std::string.
E.g.
#include <string>
#include <iostream>
int main()
{
std::string hw( "Hello, world!\n" );
std::cout << hw;
return 0;
}
Are you by any way compiling using C++/CLI, the Microsoft extension for .NET, and not standard ISO C++?
In that case you should do the following:
System::String^ MessageBoxText = CharNameTextBox->Text;
Also see the following articles:
How to: Convert Between Various String Types
How to: Convert System::String to Standard String
How to: Convert Standard String to System::String
I want to replace all the occurances of ' in a string to ^, but i saw string.replace is not the right function for me, do I need to write my own? It's boring.
You can use std::replace from <algorithm> instead of using string::replace from <string>
Sample code
#include <iostream>
#include <algorithm>
int main()
{
std::string s = "I am a string";
std::replace(s.begin(),s.end(),' ',',');
std::cout<< s;
}
Output : I,am,a,string