簡體   English   中英

"在 C++ 中將字符轉換為字符串"

[英]convert char to string in c++

我試圖通過將字符串提供給函數來減少字符串。 例如,如果我有"abrakadabra" ,該函數將返回一個字符串"abrkd" 這意味着所有字符在返回值中應該只存在一次。

我有以下功能:

string textletters(string text) {
    string ret = "";
    for (unsigned i = 0; i < text.length(); i++) {
        if (i == 0) {
            ret += str;
        } else {
            bool exist = false;
            for (unsigned j = 0; j < i; j++) {
                if (text.at(i) == text.at(j) {
                    exist = true;
                }
            }
            if (!exist) {
                ret += str;
            }
        }
    }
    return ret;
}

我知道text.at(i)會返回一個char 但我想將此char轉換為string ,以便將其連接起來。

使用push_back方法將單個字符附加到std::string 您也可以使用operator +=來做到這一點。

我提出了另一種方法:

C++ 帶有很多容器邏輯。

您可以將字符串轉換為set ,它只允許每個值有一個元素:

#include <set>
#include <algorithm>

std::string input("akrakadabra");
std::set<char> only_once(input.begin(), input.end());

這樣做的缺點是字母在之后排序,而不是按照字符串中第一次出現的順序,但您沒有指定是否需要特定順序。

您也可以使用范圍構造函數將set轉換回string

std::string result(only_once.begin(), only_once.end());

或類似。

這通常使用std::stringstream

#include <sstream>
...
std::stringstr ss;
...
ss << text.at(i);
...
std::string result = ss.str();

正如其他人所指出的,只是為了回答名義問題,實際上對這個問題沒有用:

有一個std::string構造函數,它接受一個字符和一個重復計數。 因此,要創建由單個字符組成的字符串,您可以使用重復計數為 1:

string(1, ch)

但這非常浪費,即使在具有小字符串優化的實現上也是如此。 因此,正如安德魯在他的回答中所建議的那樣,只需將您的角色直接附加到您現有的字符串中即可。

我需要復習我的 C++ 課程,但目前,我只能用 VisualBasic 代碼的例子來幫助你,它可能會給你帶來一個新的想法來實現它,看看我做了什么:

Option Explicit
Public Function DeleteRepeated(Text As String) As String
Dim length, i As Integer
length = Len(Text)
Dim NewText, Char As String
NewText = ""
For i = 1 To length
Char = Mid(Text, i, 1)
If InStr(1, NewText, Char, vbTextCompare) = 0 Then
NewText = NewText & Char
End If
Next i
DeleteRepeated = NewText
End Function

這是您要查找的內容,現在按照您的要求使用 C++,如果您有任何問題,請告訴我,希望對您有所幫助:

// Delete Repeated Char in String
#include <iostream>
#include <string>
using namespace std;

string UniqueInString (string MyString)
{
  string chr,NewText;
    for (unsigned i=0; i<MyString.length(); ++i)
    {
    chr = MyString.at(i);
    if (NewText.find(chr) == string::npos)
    {
    NewText = NewText + chr;
    }
    }
  return NewText;
}

int main ()
{
  string z = "abrakadabra";
  cout<< UniqueInString(z);
}

這意味着返回的字符序列應該具有原始順序中的字符,但如果這不是實際要求,那么解決方案很簡單:

std::string res = text;
std::sort(res.begin(), res.end());
auto last = std::unique(res.begin(), res.end());
res.erase(last, res.end());
return res;

這是 lambda 解決方案:

  string res = "abrakadabra";

  res.erase(remove_if(res.begin(), res.end(), [&res](const char& c)
  { return find(res.c_str(), &c, c) != &c; }), res.end());

  cout << res << "\n";

輸出: abrkd

C/C++ 方便的數據類型交換函數。 在這個 repo 中有所有 c++ 數據類型的便利函數,我把它們都寫在 python 中。

使用這個回購:

https://github.com/azizovrafael/Simple-CPlusPlus

...

int main(){
   int n = INT(INPUT("Write Some Text For Console Ex. (Enter: )"));
   string str = STR(n);
 
   return 0;
}

...

例子:

#simplecplusplus

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM