简体   繁体   English

如何向C++中的字符串添加字符?

[英]How to add characters to strings in C++?

I am writing an IP calculator program to convert an IP adress into the broadcast adress,.network adress etc.我正在编写一个 IP 计算器程序,将 IP 地址转换为广播地址、.network 地址等。

I need to convert the adress into 8-digit biniary first, I already used itoa for it decimal to biniary conversion, but I still need to make it always 8 digit.我需要先将地址转换为 8 位二进制,我已经使用itoa进行十进制到二进制的转换,但我仍然需要将其始终设为 8 位。

I want to use a switch to do it.我想用一个switch来做。 First the program counts the number of digits in the binary form, and then, using switch (I put it into a comment, I need to figure out this proble first) and if it adds enough zeros in front of the number (actually a string at this point) so it would always be 8 characters long.首先程序计算二进制形式的位数,然后使用switch (我把它放在注释中,我需要先弄清楚这个问题) if它在数字前面添加足够多的零(实际上是一个字符串在这一点上)所以它总是 8 个字符长。

I want to use string e1('00',e);我想使用string e1('00',e); instruction to add two zeros to the string, but it doesn't work.向字符串添加两个零的指令,但它不起作用。 e is the original string, e1 is the new 8 character string. e是原始字符串,e1 是新的 8 个字符的字符串。 It doesn't want to compile, stops at this line ( string e1('00',e); ) and gives:它不想编译,停在这一行( string e1('00',e); )并给出:

 error: no matching function for call to 'std::__cxx11::basic_string<char>:: basic_string(int, std::__cxx11::string)'|

My code:我的代码:

#include <iostream>
#include <string>
#include <bits/stdc++.h>
using namespace std;
/**
* C++ version 0.4 std::string style "itoa":
* Contributions from Stuart Lowe, Ray-Yuan Sheu,

* Rodrigo de Salvo Braz, Luc Gallant, John Maloney
* and Brian Hunt
*/
std::string itoa(int value, int base)
{

    std::string buf;

    // check that the base if valid
    if (base < 2 || base > 16)
        return buf;

    enum
    {
        kMaxDigits = 35
    };
    buf.reserve(kMaxDigits); // Pre-allocate enough space.

    int quotient = value;

    // Translating number to string with base:
    do
    {
        buf += "0123456789abcdef"[std::abs(quotient % base)];
        quotient /= base;
    } while (quotient);

    // Append the negative sign
    if (value < 0)
        buf += '-';

    std::reverse(buf.begin(), buf.end());
    return buf;
}

int main()
{
    cout << "Dzien dobry." << endl
         << "Jest OK!\n";
    int a, b, c, d, i, j, k, l, m, n, o, p, r, s, t, u, w, v, x, y, z;

    cout << "Wprowadz pierwszy oktet: ";
    cin >> a;
    cout << "Wprowadz drugi oktet: ";
    cin >> b;
    cout << "Wprowadz trzeci oktet: ";
    cin >> c;
    cout << "Wprowadz czwarty oktet: ";
    cin >> d;
    cout << "Wyswietlam adres IP w postaci dziesietnej: " << a << "." << b << "." << c << "." << d << "\n";
    char res[1000];
    itoa(a, res, 2);
    string e(res);
    itoa(b, res, 2);
    string f(res);
    itoa(c, res, 2);
    string g(res);
    itoa(d, res, 2);
    string h(res);
    //
    x = e.size();
    cout << x << "\n";
    /*
    if (x<8)
    {
        switch(x)
        {
            case 1: string e(1);
            break;
            case 2: string e(3);
            break;
            case 3: string e(2);
            break;
            case 4: string e(0);
            break;
            case 5: string e(4);
            break;
            case 6: string e(7);
            break;
            case 7: string e(0);
            break;
            default: cout << "error";
        }
    }

*/
    string e1('00', e);
    cout << e1 << "\n";
    cout << "Wyswietlam adres IP w postaci dwojkowej: " << e1 << "." << f << "." << g << "." << h;

    return 0;
}

Single quotation marks are for single characters, double quotation marks are for strings, ie more than one character, so you need "00" .单引号是针对单个字符,双引号是针对字符串,即多个字符,所以需要"00"

There is no std::string constructor for the parameters you provide, among other methods, you can use:您提供的参数没有std::string构造函数,除其他方法外,您可以使用:

string e1;
e1.append("00").append(e);

Or要么

string e1 = "00";
e1 += e;

Aditional notes:附加说明:

Avoid using #include <bits/stdc++.h>避免使用#include <bits/stdc++.h>

Avoid using using namespace std;避免使用using namespace std;

Just use + or += :只需使用++=

std::string foo = "Hello";
std::string bar = foo + " world";

std::cout << bar; // Prints Hello world

You can also modify a string in-place with += :您还可以使用+=就地修改字符串:

std::string foo = "Hello";
foo += " world"; // foo is "Hello world" now

Note that this invalidates any existing iterators you had that pointed to characters inside foo .请注意,这会使您拥有的指向foo内字符的任何现有迭代器无效。 You can get the new begin and end iterators by just calling begin and end again.您只需再次调用beginend即可获得新的beginend迭代器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM