C++入門 String 讀寫


1.從控制台 讀入 用戶輸入的單個string

特征代碼

string s;

cin>>s;

#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
int main()
{
    string s;
    cout << "Please enter a string" << endl;
    cin >> s;
    cout << "You have entered : " << s << endl;
    system("pause");
    return 0;
}

2.從控制台 持續讀入單個string 並進行字符串拼接

特征代碼

while(cin>>s){}

#include<iostream>
#include<string>
using namespace std;
int main()
{
    string s, sNew;
    cout << "Please enter somestring" << endl;
    while (cin >> sNew)
    {
        s += sNew;
        cout << "You have entered : " << s << endl;
    }
    system("pause");
    return 0;
}

string讀入的特點

1)從遇到的第一個不為空的字符串開始,即會忽略開頭的空格

2)每遇到空格就停止本次讀入

3)>>操作符返回讀入的istream對象,即用戶輸入的內容,如果用戶輸入的內容不為空(達到文件為或遇到無效輸入),那么會一直執行while循環

3.讀取整行文本

getline(*in,string){}

作用:*in 輸入流對象,如cin; 讀入的字符串保存在string對象中

特點:1)遇到換行符即停止一次讀入,即使開頭就位換行符,那樣的話,讀入的字符串就為空

2)不會忽略開頭的空格

返回值: 將不斷讀入的istream作為返回值,可以用於while的判斷中(是否還有字符串輸入)

#include<iostream>
#include<string>
using namespace std;
int main()
{
    string s;
    cout << "Please enter string" << endl;
    while (getline(cin, s))
    {
        cout << "You said:" << s<<endl;
    }
    system("pause");
    return 0;

3.string的長度

概念:string包含的字符有多少個 如 c 1 空格

語句:string s;

s.size(); 作用:返回s包含的字符個數

返回類型 string::sizy_type型

可以直接輸出s.size()

也可以用一個string::size_type類型的變量接收s.size()

string::size_type是unsigned類型中的一員

示例程序:

1)

#include<iostream>
#include<string>
using namespace std;
int main()
{
    string s;
    cout << "Please enter the string:" << endl;
    cin >> s;
    cout << "You have entered: " << s << endl;
    cout << "The length of the string is: " << s.size()<< endl;
    system("pause");
    return 0;
}

2)

#include<iostream>
#include<string>
using namespace std;
int main()
{
    string s;
    cout << "Please enter the string:" << endl;
    cin >> s;
    string::size_type size = s.size();
    cout << "You have entered: " << s << endl;
    cout << "The length of the string is: " << size<< endl;
    system("pause");
    return 0;
}

注意 如果在s讀入前就定義size=s.size();

則默認的size大小為0,讀入后size也沒有改變為字符串大小

?原因未知? 像這樣:

3)

#include<iostream>
#include<string>
using namespace std;
int main()
{
    string s;
    string::size_type size = s.size();
    cout << "Please enter the string:" << endl;
    cin >> s;
    cout << "You have entered: " << s << endl;
    cout << "The length of the string is: " << size<< endl;
    system("pause");
    return 0;
}

4)string size 包括空格

#include<iostream>
#include<string>
using namespace std;
int main()
{
    string s;
    cout << "Please enter the string:" << endl;
    getline(cin,s);
    cout << "You have entered: " << s << endl;
    cout << "The length of the string is: " << s.size()<< endl;
    system("pause");
    return 0;
}

用getline 開頭的空格會被讀入string , size里包括空格字符

 4.讀取字符串中的字符

途徑:通過下標獲取

下標數據類型: size_type

示例一:判斷字符串的開頭字母是什么(聯系OpenGL Object文件讀入時判斷數據類型)

#include<iostream>
#include<string>
using namespace std;
int main()
{
    string s;
    cout << "Please enter the string:" << endl;
    //string::size_type index;
    while (getline(cin, s))
    {
        cout << "You have entered: " << s << endl;

        if (s[0] == 'v')
            cout << "This data is a vertex " << endl;
        else
            if (s[0] == 'f')
                cout << "This data is a face" << endl;
            else
                cout << "Dont't konw what is this." << endl;
        cout << "Please enter the string:" << endl;

    }
    system("pause");
    return 0;
}

示例二:

數一數一個字符串里有多少個字符x

#include<iostream>
#include<string>
using namespace std;
int main()
{
    string s;
    int num_x = 0;
    cout << "Please enter the string:" << endl;
    cin >> s;
    for (string::size_type i = 0; i < s.size(); i++)
        if (s[i] == 'x')
            num_x++; 
            cout << "There are " << num_x << " \" x \" in the string." << endl;
        
    system("pause");
    return 0;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM