Jaka jest różnica między zmienna char a string ?
Taka, że char
to znak, a string
to ciąg(łańcuch) znaków.
UWAGA uczymy sie samodzielnego szukania informacji UWAGA
- wchodzimy na google.com
- wpisujemy cos na zasadzie takiej difference between char and string c++
- mamy pierdyliard wynikow
- czytamy dokumentacje jezeli to nie rozwiaze problemu
ale np. przy definicji
char abc = "abecadło"
też można przy użyciu char
dodać string
dodanie znacznika <code class="cpp"> - furious programming
nowicjusz35 napisał(a):
ale np. przy definicji char abc = "abecadło"
to Ci się po prostu nie skompiluje
czemu nie mogę takiego czegoś skompilować jak na filmie na youtube normalnie kompiluje:
#include <iostream>
using namespace std;
string login,haslo;
int main()
{
cout << "Podaj login:";
cin >> login;
system("pause");
}
char
- ang. character, tłumaczenie: znak
string
- tłumaczenie: ciąg
String w C++ (źródło: http://www.cplusplus.com/reference/string/string/):
String class
Strings are objects that represent sequences of characters.The standard string class provides support for such objects with an interface similar to that of a standard container of bytes, but adding features specifically designed to operate with strings of single-byte characters.
The string class is an instantiation of the basic_string class template that uses char (i.e., bytes) as its character type, with its default char_traits and allocator types (see basic_string for more info on the template).
Note that this class handles bytes independently of the encoding used: If used to handle sequences of multi-byte or variable-length characters (such as UTF-8), all members of this class (such as length or size), as well as its iterators, will still operate in terms of bytes (not actual encoded characters).
Null-terminated-string w C, (źródło: http://en.wikipedia.org/wiki/Null-terminated_string):
In computer programming, a null-terminated string is a character string stored as an array containing the characters and terminated with a null character ('\0', called NUL in ASCII). Alternative names are C string, which refers to the C programming language and ASCIIZ (note that C strings do not imply the use of ASCII).
The length of a C string is found by searching for the (first) NUL byte. This can be slow as it takes O(n) (linear time) with respect to the string length. It also means that a NUL cannot be inside the string, as the only NUL is the one marking the end.
To powinno znaleźć się w innym temacie, ale już tutaj odpowiem
nowicjusz35 napisał(a)
czemu nie mogę takiego czegoś skompilować jak na filmie na youtube normalnie kompiluje
Dołączyłeś nagłówek ze standardowymi strumieniami wejścia-wyjścia. Aby używać ciągów znaków z C++ (string), musisz dołączyć odpowiedni nagłówek, tj.
#include <string>
Jeszcze w ramach ciekawostki (http://ideone.com/HlnBoT):
Mimo iż
stringi
w programowaniu są wyspecjalizowane w przechowywaniu tekstu, to nic nie stoi na przeszkodzie, aby używać innych typów danych niżznak
.
#include <iostream>
#include <string>
using namespace std;
int main() {
typedef int number;
typedef basic_string<number> numeric_string;
int nullTerminatedArrayOfNumbers[] = { 1, 2, 3, 4, 123, 0 /* or NULL */ };
numeric_string ns = nullTerminatedArrayOfNumbers;
ns.push_back(997);
for(auto &number : ns)
cout << number << " ";
return 0;
}
out: 1 2 3 4 123 997