简体   繁体   English

如何修剪矢量char数组或字符串?

[英]how to trim vector char array or string?

I am following this code to trim 我正在按照此代码进行修剪

    std::string tcp_read(int listen_at_port=8001){

    using namespace std;
    using namespace boost::algorithm;
    std::vector<char> received_data(512); 
    tcp_read(received_data, listen_at_port);
    string str1(received_data.begin(),received_data.end());
    trim_right(str1);
    return str1;
}

I stepped throught the code, my received_data is generally = "add 8002 (here onwards buffer is filled with spaces till the [511th] position)" 我逐步完成了代码,我的received_data通常=“添加8002(此处缓冲区填充空格直到[511th]位置”)

now when I did trim_right I expected the str1 size to become 8, but its still 512 when its returning, why? 现在当我做了trim_right时,我预计str1的大小会变为8,但是当它返回时仍然是512,为什么呢?

How do i get actually trim and change the size so that the string just accomodates till the last non space character 我如何实际修剪并更改大小,以便字符串可以容纳直到最后一个非空格字符

As explained in the other answer , your vector is filled with zeros, which are not considered whitespace , so trim_right doesn't strip those from the string. 正如在另一个答案中所解释的那样,你的vector用零填充,它们不被认为是空格 ,因此trim_right不会从字符串中trim_right那些。 An easy fix to your problem is to not construct the string using the begin and end iterators of the vector, but use the string( char const * ) constructor. 解决问题的一个简单方法是不使用向量的开始和结束迭代器构造字符串,而是使用string( char const * )构造函数。 This also eliminates the call to boost::trim_right . 这也消除了对boost::trim_right的调用。

std::string tcp_read(int listen_at_port=8001)
{
    using namespace std;

    std::vector<char> received_data(512); 
    tcp_read(received_data, listen_at_port);

    return string( received_data.data() );
    // or return string( &received_data[0] );
}

trim_right only removes all trailing whitespaces. trim_right仅删除所有尾随空格。 When you declare received_data it is initialized with all zeros. 当您声明received_data它将使用全零进行初始化。 When you read data from the socket the data at the end of the buffer is [most certainly] not composed of spaces. 当您从套接字读取数据时,缓冲区末尾的数据[当然]不是由空格组成。

I suggest that when you call tcp_read() you return the number of bytes that were actually read from the socket and do recieved_data.resize(byte_count); 我建议你在调用tcp_read()时返回从套接字实际读取的字节数并执行recieved_data.resize(byte_count); instead of calling trim_right . 而不是调用trim_right Also if the data received over the socket can contain zeros you should probably be returning a vector instead of string . 此外,如果通过套接字接收的数据可以包含零,则应该返回vector而不是string

[Kudos to Praetorian for pointing out that it removes all whitespaces ] [感谢Praetorian指出它删除了所有空格 ]

That's silly, but in case whitespaces aren't really whitespaces: 这很愚蠢,但是如果空白不是真正的空白:

string str1(received_data.begin(), 
    find(received_data.begin(), received_data.end(), received_data[511]) );

And if they are: 如果它们是:

string str1(received_data.begin(), 
    find( 
        find(received_data.begin(), received_data.end(), ' ') + 1, 
        received_data.end()) );

But that's just some hacks, trim right should work just fine, there's probably something wrong with the received_data. 但这只是一些黑客,修剪正确应该工作得很好,receive_data可能有问题。

And yes, trimming the string wouldn't reallocate any memory anyway, so it might be ok to store it as it is. 是的,修剪字符串无论如何都不会重新分配任何内存,因此可以按原样存储它。

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

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